How do I link two collections from a model to a view? - c#

I have a model defined:
public class ExhibitionItemModel
{
public IEnumerable<tblItem> Items { get; set; }
public tblExhibition Exhibition { get; set; }
public IEnumerable<tblExhibitionItem> ExhibitionItems { get; set; }
}
tblItem contains information about a specific item, e.g., description, code, etc.
tblExhibitionItem contains an id from tblItem, an exhibition id tells the system for which exhibitionm a specific item belongs, and a price for that item/exhibition combination.
In my Controller, I am populating ExhibitionItemModel:
ExhibitionItemModel exhibitionItemModel = new ExhibitionItemModel();
exhibitionItemModel.Exhibition = db.tblExhibitions.Find(id);
exhibitionItemModel.ExhibitionItems = (from objExhibitionItems in db.tblExhibitionItems
where objExhibitionItems.ExhibitionID == id
select objExhibitionItems).AsEnumerable<tblExhibitionItem>();
exhibitionItemModel.Items = (from objItem in db.tblItems
select objItem).OrderBy(item => item.Code).AsEnumerable<tblItem>();
return View(exhibitionItemModel);
Im my view, I list out all the items and I want to have a checkbox which is selected according to whether the item is in tblExhibitionItem. Then a textbox to enter the price for the item.
So far, I have:
#foreach (var item in Model.Items)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Code)</td>
<td>#Html.DisplayFor(modelItem => item.Description)</td>
<td>#Html.CheckBoxFor(modelItem => item.AdditionalItem.Value, new { #disabled = "disabled" })</td>
<td>#Html.CheckBox("chkIsSelected")</td>
<td>#Html.Editor("txtPrice")</td>
</tr>
}
As I am a newbie to MVC, I am stuck as to how I establish the link between the checkbox and the Price textbox for an ExhibitionItem to that of the Item. In other words, when I commit the changes to a database, I am populating the ExhibitionItems with the ID of the Item and the Price, depending on whether the Checkbox ("chkIsSelected") is selected.
Additionally, what do I need to change to get the checkbox and Price populated if there are already exhibitionitems in the database?

You can achieve this using ajax.
You need to figure out what checkbox is for what value. So the best way to link it is by using the ID.
So your checkbox code may look like this:
<td>#Html.CheckBox("chkIsSelected", new { #value = item.ID })</td>
Now you'd use jquery to get an array of selected checkbox ids, and pass that to your controller.
$('#submitButton').click(function(e) {
e.preventDefault();
var selectedItems = new Array(); // Create Array
// Add to array
$('#chkIsSelected:checked').each(function() { selectedItems.push($(this).val()); });
$.ajax({
url: '/update-exhibition-item',
data: { items: selectedItems },
traditional: true,
success:function (data) {
// do something.
});
});
});
Now you have to read this into your controller. This could be a partialview, or a proper view with results. It's up to you. Regardless, you'd cycle through the array and update each value that exists in the array. e.g.
[ActionName("update-exhibition-item")]
public ActionResult UpdateExhibition(int[] items)
{
var list = from a in tableA
where items.Contains(a.id)
select a;
// etc.
}

Related

Can't get DropDownList working in .NET (C#)

I'm still pretty new to .NET, but I think I've read everything there is to read on this subject (including similar questions on SO, which is where I got some of the things I've tried). I feel like I've tried everything possible and I still can't get it to work.
I have a Note class and a Category class. Pretty straightforward, each note has a Category property, so I want to have a dropdown list in my Create view that displays categories. I can get a list to display the category names correctly, but that's it. It keeps telling me there's no IEnumerable in my ViewData called "Categories" when there definitely, 1000% for sure is...
The Create action in my NoteController looks like this:
// GET: Create
public ActionResult Create()
{
SelectList items = (new CategoryService()).GetCategories().Select(c => new SelectListItem
{
Value = c.CategoryId.ToString(),
Text = c.Name
}) as SelectList;
ViewData["Categories"] = items;
return View();
}
And I've tried a few variations in the view:
#Html.DropDownListFor(e=>e.CategoryId , (IEnumerable<SelectListItem>) ViewData["Categories"])
#Html.DropDownList("Categories", "Select a Category")
My Create view uses a NoteCreate model, which has this:
public class NoteCreate {
...
[Display(Name = "Category")]
[Required]
public string CategoryId { get; set; }
And my NoteService has a CreateNote method like so:
public bool CreateNote(NoteCreate model)
{
using (var ctx = new ApplicationDbContext())
{
bool isValid = int.TryParse(model.CategoryId, out int id);
if (!isValid)
{
id = 0;
}
var entity =
new Note()
{
OwnerId = _userId,
Title = model.Title,
Content = model.Content,
CreatedUtc = DateTimeOffset.Now,
Status = model.Status,
CategoryId = id
};
ctx.Notes.Add(entity);
return ctx.SaveChanges() == 1;
}
}
I figured I have to turn the ID into a string for the sake of the dropdown list (because SelectListItem's Value and Text are strings), which is why I parse it back into an int here
I tried attaching the list to the ViewBag instead, and I've tried variations of both DropDownListFor and DropDownList
One of those combinations resulted in a dropdown list actually showing, and I don't remember what it was, but selecting an item resulted in a null being passed to the NoteCreate method (model.CategoryId)
Can anyone help me, and potentially many others who will struggle with this in the future because the documentation is so terrible?
UPDATE:
My controller has been refactored to:
// GET: Create
public ActionResult Create()
{
List<SelectListItem> li = new List<SelectListItem>();
List<Category> Categories = (new CategoryService()).GetCategories().ToList();
var query = from c in Categories
select new SelectListItem()
{
Value = c.CategoryId.ToString(),
Text = c.Name
};
li = query.ToList();
ViewBag.Categories = li;
return View();
}
and my view has been refactored to:
#Html.DropDownList("Categories", ViewBag.Categories as SelectList, new { #class = "form-control" })
This is closer, as I can now load the view and see the Category names in the dropdown. However, when I save, model.CategoryId in my CreateNote method is null, so the CategoryId value isn't actually being passed from the dropdown into the model.
If ViewModel is used in the view then its better to paa the data through model properties to the view. No need to put the collection for Dropdownlist in ViewData or ViewBag.
For the detail way of using Dropdownlist through SelectList and pass to the view through, I would refer an answer I had posted:
MVC C# Dropdown list Showing System.Web.SelectListItem on the model and can not blind to controller
The model passed to your view needs a property for CategoryId.
Your Html Helper is looking for CategoryId here:
#Html.DropDownListFor(e=>e.CategoryId
Ok... I figured it out.
It's so stupid.
The key you use to store the SelectList in your ViewData HAS to be the same as the name of the property on the model, even though you can explicitly tell it to use the list using a different key....
So even if you wanted to use the same SelectList for a few different properties (but process them differently in your service, say), you'd have to pass it to the ViewData redundantly for each property
So instead of passing my SelectList through as ViewBag.Categories, I passed it in as ViewBag.CategoryId, and that worked.
I'm going to go drink a lot of alcohol now.
In Controller
List<SelectListItem> li = new List<SelectListItem>();
var query = from of in your_context.Categories
select new SelectListItem()
{
Value = of.CategoryId.ToString(),
Text = of.Name
};
li = query.ToList();
ViewBag.Category_ = li;
View
<div class="col-md-10">
#Html.DropDownList("Categories", ViewBag.Category_ as List<SelectListItem>, new { #class = "form-control" })
</div>

Use linq to check if the contents of an array are in a column in the database

I have a function that returns a list of ids based on a separate selection. This function returns all the ids and they are displayed as checkboxes in the view. Once I select some of the checkboxes, they are saved to the database as a comma delimited string. When editing the selections, the user needs to be able to deselect selected check boxes and select alternate ones. The issue I have is how to set the checkboxes as checked that exist in comma delimited string in the database. I have a linq statement that creates an array of strings, but I'm not sure how to check any of the checkboxes that are in the array based on their ID.
Here are the functions in the ViewModel:
public IEnumerable<SelectListItem> DocNamesByDocTypeIdList()
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
docName = (from t in db.vwMapDocNamesToSecurityUsers
select new
{
t.DocName,
t.DocNameId,
t.DocTypeId
}).Distinct()
.Select(x => new SelectListItem()
{
Text = x.DocName,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocTypeId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
var docCount = docName.Count();
return docName;
}
}
public String[] GetDocNamesForDocTypeId(int? docTypeId)
{
var nameCkBoxes = DocNamesByDocTypeIdList().Where(m => m.Group.Name == docTypeId.ToString()).ToString().Split(',');
//Set the ids in the array as checked in the view?
return nameCkBoxes;
}
Foreach loop in the view:
<div class="ckDocNames">
#foreach (var dn in Model.GetDocNamesForDocTypeId(Model.DocTypeId))
{
<div class="checkboxContainer, editCkBoxes">
<input class="ckBoxes" type="checkbox" name="DocNameId" value="#dn.Value" dtid="#dn.Group.Name" />#dn.Text<br />
</div>
}
</div>
from your previous question and also from my answer there :)
you can
public IEnumerable<int> GetDocNamesForFilterId(int id)
{
using (var db = new ARXEntities())
{
var selectedIds = (from t in db.ServiceAccountFilters
where t.FilterId == id
select t.DocNameId).ToList();
return selectedIds;
}
}
and in the FilterModel add new property for the selectedIds
public IEnumerable<int> SelectedDocNames { get; set; }
and in the GET Edit Action add this
var model = new FilterViewModel
{
...
SelectedDocNames = GetDocNamesForFilterId(serviceAccountFilter.FilterId);
};
and in your view you should populate all checkboxes and just check what is choosen before like below to let the user check or uncheck any one in the edit mode
#foreach (var dn in Model.DocNamesByDocTypeIdList())
{
<div class="checkboxContainer">
<input class="ckBoxes" type="checkbox" name="selectedDocIds" checked="#Model.SelectedDocNames.Contains(int.Parse(dn.Value))" value="#dn.Value" dtid="#dn.Group.Name" />#dn.Text<br />
</div>
}
and in the POST action just check what are selected by the user as below
var selectedIds = form.GetValues("selectedDocIds");

C# MVC DropDownListFor List of Strings

I am working with a list of string items in mvc that needs to be selected from a drop down list. The drop down list is binding fine, and value's are setting fine, but even though the current item being iterated matches an item in the drop down list it isn't being pre-selected as it's value, can anyone point me in the right direction?
#for (var i = 0; i < Model.StringList.Count; i++)
{
if (BL.Helpers.StringHelpers.Validate(Model.DisplayStringSegments[i]))
{
<div id="editor-field">
#Html.DropDownListFor(m => m.StringList[i], Model.PosterOptions, String.Empty, new { })
</div>
}
else
{
<div id="editor-label">#Model.StringList[i]</div>
#Html.HiddenFor(model => model.StringList[i])
}
}
So for this case, the Options is a list of strings holding only one value, "Test" -> set both as Text and Value;
PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test" });
Can anyone tell me why the current StringList[i] isn't being pre selected, even though it has the value of "Test" ?
For anyone that comes across this;
I had to "Hack" a solution, I did this by:
Changing my ViewModel's (Model.Options)
List<SelectListItem> to a List<string>
Changing my drop down list selection to the following, forcing the selected value;
<div id="editor-field">
#{
string currentString = Model.StringList.ElementAt(i).ToString();
}
#Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, currentString), String.Empty, new {})
</div>
Perhaps there is a better way, but this works!
Another way could be setting the current selected item during the list creation, like this:
PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test", Selected = true });
I had the same issue and your response helped me. I don't think that's a "hack" though. Because in your question you were using the same SelectList for all the dropdownlists so even though you mention you didn't want to create multiple lists for the drop downs I can't see another way when you have multiple drop downs as you need to specify different selected values.
As a small refactoring you can get rid of the temp variable and access the selected value directly like this:
#Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, Model.StringList[i]), String.Empty, new {})
In your example you don't need to distinguish between text and value but in my case it was required. When that's necessary it can be accomplished by providing the value and text field names for the SelectList. For example, say you need multiple dropdowns with Country values such as:
Country class:
public class Country
{
public string Code { get; set; }
public string Name { get; set; }
}
Model:
public List<string> CustomerCountryList { get; set; }
public IEnumerable<Country> CountryList { get; set; }
and the View:
#Html.DropDownListFor(m => m.CustomerCountryList[i], new SelectList(Model.CountryList, "Code", "Name", Model.CustomerCountryList[i]))

Show selected items in a multi-select list box

I have tried various ways to get this to work. I think maybe I have something missing in my model or controller, so I'm posting all three parts.
I have data in a database that shows some advising topics chosen by an advisor in a previous appointment. When the advisor calls up that appointment, the app should display a list of all possible topics with the ones previously chosen highlighted. Everything works except that last bit.
I know I'm retrieving the right information because I can display the selected items separately. I just can't get them to show up selected. Here's the code. I'm cutting out irrelevant parts.
public class AppointmentModel
{ ...
public string AdvisingTopicId { get; set; }
public List<SelectListItem> AdvisingIdList { get; set; }
public SelectList AdvisingTopicNames { get; set; }
}
public class HomeController : AdvisorBaseController
{ ...
var topicCodes = appointment.advising_topic.ToList();
var advisingTopics = new SelectList((from t in topicCodes
select t.name).ToList(), "name");
var topicsList = (from t in db.advising_topic
select new SelectListItem
{
Selected = false,
Text = t.name,
Value = SqlFunctions.StringConvert((double)t.advising_topic_id).Trim()
}).ToList();
foreach (var topicCode in topicCodes)
{
var selTopic = topicsList.Find(x => x.Value == topicCode.advising_topic_id.ToString());
if (selTopic != null)
{
selTopic.Selected = true;
}
} ...
var appointmentModel = new AppointmentModel
{ ...
AdvisingTopicNames = advisingTopics,
AdvisingIdList = topicsList,
};
and then the view
#model AcademicAdvising.Models.AppointmentModel
<h3>Advising Topics</h3>
<ul>
#foreach (var item in Model.AdvisingTopicNames)
{
<li>#Html.DisplayFor(x => item)</li>
}
</ul>
#Html.ListBoxFor(m=>m.AdvisingIdList, new SelectList(Model.AdvisingTopicNames, "Value", "Text", Model.AdvisingTopicNames.SelectedValue))
Note that the foreach correctly displays the selected items. That's just for testing and will be pulled out. the ListBoxFor is where I'm struggling. What I have here doesn't work (shows the full list with nothing highlighted). And that's the bit where I have tried various approaches, with all fails.
It looks like you may have accidentely went a level too deep. You already have a select list which is all the listboxfor function wants.
#Html.ListBoxFor(m=>m.AdvisingIdList, Model.AdvisingTopicNames)
But honestly looking at how you are defining your lists I think what you really want might be
#Html.ListBoxFor(m=>m.AdvisingIdList, Model.AdvisingIdList)

How to get the selected list of items from checkboxlist and pass it to the Action in MVC4

I am trying to implement a checkboxlist in my MVC application. here I need to get the selected checkboxlist values pass it to #html.ActionLink().
I have searched many sites including Stackoverflow but there is no model binding of database values and getting the selected checkbox values and passing to an action.
Eg :https://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0
and I tried from this link. CheckBoxList multiple selections: how to model bind back and get all selections?
How to get the selected productid from the checkbox list.
Any examples.. ??
view:
#foreach (var item in Model.Tags)
{ <label>
<input type="checkbox" name="tag" value="#item.TagID" #if (item.Selected) { <text>checked="checked"</text> } />#item.Name
</label>
}
controller:
[HttpPost]
public RedirectToRouteResult Edit(IEnumerable<Guid> tag)
{
using (var dc = new MyDataContext())
{
var existing = dc.Tag
.Select(i => i.TagID)
.ToList();
// remove old
foreach (var id in existing.Except(tags.EmptyIfNull()))
dc.Tag.DeleteOnSubmit(dc.Tag.Single(k => k.TagID == id);
// add new
foreach (var id in tags.EmptyIfNull().Except(existing))
dc.Tag.InsertOnSubmit(new Tag() { TagID = id, });
dc.SubmitChanges();
}
return RedirectToAction("List");
}

Categories