how to set the selected value in the drop down list populated with ViewData in mvc - asp.net-mvc-4

I am new to MVC, please help me to set the selected value in drop down list populated using View Data. I have gone through so many solutions but every solutions deals with selecting value for single drop down. I have same drop down listed using the foreach loop. Setting selected value for each dropdown in that foreach loop.
My code is shown below.
[In view]
int i = 0;
foreach (var item in Model.Select((x, j) => new { Data = x, Index = j + 1 })) {
#Html.DropDownListFor(model => item.Data.CategoryID,(IEnumerable<SelectListItem>)ViewData["categories"], new { #id = "category" + i })
i++;
}
[in controller]
SelectList selectList = new SelectList((IEnumerable<Category>)ConvertCategoryToList(dt1), "CategoryID", "CategoryName");
ViewData["categories"] = selectList;

There is a lot missing from your sample (e.g.what a category looks like, what the Edit actions look like, what is dt1, what the model is you pass to the view etc), but I will try based on what is shown.
Problem 1: Can't use foreach in binding
You can't use a foreach with a collection of form items. The items have no way of knowing which individual element to bind with (when they are sent back to the server they need more information to make each entry unique).
Solution:
It only knows how to bind if you use indexing (e.g. using for (int i=0; < count; i++){ #Html.DropDownListFor(model=>item[i]...)
Problem 2: Selected values only can come from SelectList!
This is the most irritating feature of DropDownListFor and appears to be a bug. It will not take the current selected value from the bound data. It will only take a current value from a SelectList, which means you need a unique SelectList for every dropdown.
Solution:
Create a unique SelectList in the view, per drop down required, with the item value as the current selection.
With some faking of your data, I got the following working:
Example
Controller:
Only store the list of items for the SelectList in the ViewBag:
ViewData["categories"] = ConvertCategoryToList(dt1);
View:
1. You need to pass a List and not an IEnumerable as IEnumerable cannot be indexed.
#model List<MyApplication.Item>
2. Create a SelectList for each dropdown
SelectList newSelectList = new SelectList((IEnumerable<MyApplication.Category>)ViewData["categories"], "CategoryID", "CategoryName", Model[i].Id);
3. Use the indexing method for addressing your items in the collection.
#Html.DropDownListFor(m => Model[i].Id, newSelectList, "Select an item")
Problem 3: What data to pass?
The other issue with your code, as-it-stands, is what to pass back and forth to/from the edit view? At the moment I would guess (from the variable name dt1) you are passing a DataTable(?). You will need to be very explicit about the data you are using in order to get a clean solution to this one. I would suggest posting a second question with all your code and Razor view HTML.
If you need to see more of my sample code (below), please post your own code so I can make them consistent.
Full dummy controller code below
public class TestController : Controller
{
public List<Category> dt1 { get; set; }
public TestController()
{
this.dt1 = new List<Category>();
for (int i = 1; i <= 10; i++)
{
this.dt1.Add(new Category() { CategoryId = i, CategoryName = string.Format("Category {0}", i) });
}
}
[HttpGet]
public ActionResult Edit()
{
//SelectList selectList = new SelectList((IEnumerable<Category>)ConvertCategoryToList(dt1), "CategoryID", "CategoryName");
ViewData["categories"] = ConvertCategoryToList(dt1);
List<Item> items = new List<Item>();
items.Add(new Item(){Id = 1});
items.Add(new Item(){Id = 2});
items.Add(new Item(){Id = 3});
items.Add(new Item(){Id = 4});
items.Add(new Item(){Id = 5});
return View(items);
}
[HttpPost]
public ActionResult Edit(FormCollection form)
{
// Turn form submission back into a compatible view model
List<Item> items = new List<Item>();
foreach (string key in form.Keys)
{
string val = form[key];
items.Add(new Item() { Id = int.Parse(val) });
}
// Recreate the select list
ViewData["categories"] = ConvertCategoryToList(dt1);
return View(items);
}
List<Category> ConvertCategoryToList(IEnumerable<Category> dt)
{
return dt.ToList();
}
}
Note: The post version of Edit simply recreates the list of data (using the selected values posted back) and returns to the Edit view. This is just for testing.
Screen shot
Dummy category class
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
Full test View
#model List<MyApplication.Item>
#using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
SelectList newSelectList = new SelectList((IEnumerable<MyApplication.Category>)ViewData["categories"], "CategoryID", "CategoryName", Model[i].Id);
#Html.DropDownListFor(m => Model[i].Id, newSelectList, "Select an item")
}
<input type="submit" value="Submit"/>
}

Related

I want to keep 2 values for next save from preivous save. MVC 4

User enters PWS (public water system), LabID. Then clicks Save button.
I would like these values to populate the new input form that right now gets emptied out on a succesful save.
#Html.TextBoxFor(model => model.PWS, new { #autofocus = "autofocus", #style="width:50px", #maxlength="5" })
Controller ActionResult
First time through:
[HttpGet]
public ActionResult AddColiform(string sortorder)
{
int batchid;
batchid = Convert.ToInt32(Session["ThisBatch"]);
//Session["ThisBatch"] = batchid;
ViewBag.Methods = FillMethods();
ViewBag.Latest = (from m in _db.BactiBucket
where m.Batch_ID == batchid
select m).ToList();
ViewBag.ThisBatch = batchid;
return View(new BactiBucket());
}
When Save button clicked:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddColiform(BactiBucket bucket)
{
if (ModelState.IsValid)
{
//FIRST RECORD SAVED FOR USER CREATES BATCH, OTHERWISE BATCH IS ZERO
if (Session["ThisBatch"].Equals(0))
{
var newbatchid = CheckAndMakeBatchIfNone();
Session["ThisBatch"] = newbatchid;
bucket.Batch_ID = newbatchid;
}
_db.AddToBactiBucket(bucket);
_db.SaveChanges();
return RedirectToAction("AddColiform");
}
ViewBag.Methods = FillMethods();
int batchid;
batchid = Convert.ToInt32(Session["ThisBatch"]);
ViewBag.ThisBatch = batchid;
ViewBag.Latest = (from m in _db.BactiBucket
where m.Batch_ID == batchid
select m).ToList();
return View(bucket);
}
You can pass additional parameters to your GET method in the redirect, and use ths values to set the properties of your model (note its not clear why your method has a parameter string sortorder when you never use it)
[HttpGet]
public ActionResult AddColiform(string sortorder, string PWS, string LabID)
{
....
BactiBucket model = new BactiBucket() { PWS = PWS, LabID = LabID };
return View(model);
}
[HttpPost]
public ActionResult AddColiform(BactiBucket bucket)
{
if (ModelState.IsValid)
{
....
return RedirectToAction("AddColiform", new { PWS = bucket.PWS, LabID = bucket.LabID });
}
....
}
Ok, if it was a snake it would have bit me.
In the declaration of the ActionResult I pass the values of the textboxes to the controller. It comes in with the Post action. (PWS and LabID are the names of the inputs).
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddColiform(BactiBucket bucket, string PWS, string LabID)
Then right before the return RedirectToAction("AddColiform");
I set Session variables for each value:
Session["PWS"]=PWS;
Session["LabID"]=LabID;
of course I might use ViewBag.PWS and ViewBag.LabID
Then, when returning and building the new Add Record form,
I populate the #Value of each textbox respectfully:
#Html.TextBoxFor(model => model.PWS, new {#Value=Session["PWS"], #autofocus = "autofocus", #style="width:50px", #maxlength="5" })
#Html.TextBoxFor(model => model.LabID, new {#Value=Session["LabID"], #style="width:150px", #maxlength="20" })
Since I haven't run this code I know I will have to check if Session objects aren't null. Or ViewBag objects. Or set them to "" first time through.
I got this from this forum thread

MVC4 ViewData.TemplateInfo.HtmlFieldPrefix generates an extra dot

I'm trying to get name of input correct so a collection of objects on my view model can get bound.
#{ViewData.TemplateInfo.HtmlFieldPrefix = listName;}
#Html.EditorFor(m => m, "DoubleTemplate", new {
Name = listName,
Index = i,
Switcher = (YearOfProgram >= i +1)
})
As you can see here, I pass in the "listName" as the prefix for my template, the value of listName = "MyItems".
And here is my template:
#model Web.Models.ListElement
#if (ViewData["Switcher"] != null)
{
var IsVisible = (bool)ViewData["Switcher"];
var index = (int)ViewData["Index"];
var thisName = (string)ViewData["Name"] + "[" + index + "].Value";
var thisId = (string)ViewData["Name"] + "_" + index + "__Value";
if (IsVisible)
{
#*<input type="text" value="#Model.Value" name="#thisName" id ="#thisId" class="cell#(index + 1)"/>*#
#Html.TextBoxFor(m => m.Value, new { #class ="cell" + (index + 1)})
#Html.ValidationMessageFor(m => m.Value)
}
}
but I found that the generated name becomes this: MyItems.[0].Value
It has one extra dot. How can I get rid of it?
Incidentally, I tried to manually specify the name inside the template and found the name gets overridden by the Html helper.
Update
The reason why I have to manually set the HtmlFieldPrefix is the property name (MyItems which is a list of objects) will get lost when MyItems is passed from main view to the partial view. By the time, the partial view called my template and passed in one object in MyItems, the template itself has no way to figure out the name of MyItems as it has been lost since the last "pass-in".
So that's why I have to manually set the html field prefix name. And I even tried to use something similar to reflection(but not reelection, I forgot the name) to check the name of passed in object and found it returned "Model".
Update 2
I tried Stephen's approach, and cannot find the html helper PartialFor().
I even tried to use this in my main view:
Html.Partial(Model, "_MyPartialView");
In Partial View:
#model MvcApplication1.Models.MyModel
<h2>My Partial View</h2>
#Html.EditorFor(m => m.MyProperty)
Here is my templat:
#model MvcApplication1.Models.ListElement
#Html.TextBoxFor(m => m.Value)
Here is my Model:
public class MyModel
{
private List<ListElement> myProperty;
public List<ListElement> MyProperty
{
get
{
if (myProperty == null)
{
this.myProperty = new List<ListElement>() { new ListElement() { Value = 12 }, new ListElement() { Value = 13 }, new ListElement() { Value = 14 }, new ListElement() { Value = 15 }, };
}
return this.myProperty;
}
set
{
this.myProperty = value;
}
}
}
public class ListElement
{
[Range(0, 999)]
public double Value { get; set; }
}
And here is my controller:
public ActionResult MyAction()
{
return View(new MyModel());
}
It only generates raw text("12131415") for me, instead of the wanted text box filled in with 12 13 14 15.
But if I specified the template name, it then throws an exception saying:
The template view expecting ListElement, and cannot convert
List<ListElement> into ListElement.
There is no need set the HtmlFieldPrefix value. MVC will correctly name the elements if you use an EditorTemplate based on the property type (and without the template name).
Assumed models
public class ListElement
{
public string Value { get; set; }
....
}
public class MyViewModel
{
public IEnumerable<ListElement> MyItems { get; set; }
....
}
Editor template (ListElement.cshtml)
#model YourAssembly.ListElement
#Html.TextBoxFor(m => m.Value)
Main view
#model YourAssembly.MyViewModel
...
#Html.EditorFor(m => m.MyItems) // note do not specify the template name
This will render
<input type="text" name="MyItems[0].Value" ...>
<input type="text" name="MyItems[1].Value" ...>
....
If you want to do this using a partial, you just can pass the whole model to the partial
MyPartial.cshtml
#model #model YourAssembly.MyViewModel
#Html.EditorFor(m => m.MyItems)
and in the main view
#Html.Partial("MyPartial")
or create an extension method
public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression, string partialViewName)
{
string name = ExpressionHelper.GetExpressionText(expression);
object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
var viewData = new ViewDataDictionary(helper.ViewData)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = name }
};
return helper.Partial(partialViewName, model, viewData);
}
}
and use as
#Html.PartialFor(m => m.MyItems, "MyPartial")
and in the partial
#model IEnumerable<YourAssembly.ListElement>
#Html.EditorFor(m => m)
Call your partial this way:
#Html.Partial("_SeatTypePrices", Model.SeatTypePrices, new ViewDataDictionary
{
TemplateInfo = new TemplateInfo() {HtmlFieldPrefix = nameof(Model.SeatTypePrices)}
})
Partial view:
#model List
#Html.EditorForModel()
Editor template implementation:
#using Cinema.Web.Helpers
#model Cinema.DataAccess.SectorTypePrice
#Html.TextBoxFor(x => Model.Price)
This way your partial view will contain list of items with prefixes.
And then call EditorForModel() from your EditorTemplates folder.
I found that I can change the value of HtmlFeildPrefix in my template.
So what I did to solve my problem was just to assign the correct value to HtmlFeildPrefix in the template directly rather than in the page which calls the template.
I hope it helps.
If I want to pass the HtmlFieldPrefix I use the following construct:
<div id="_indexmeetpunttoewijzingen">
#Html.EditorFor(model => model.MyItems, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo()
{
HtmlFieldPrefix = "MyItems"
}
})
</div>

Optimization of foreach statement, create new mapped Viewmodel list

Can someone please have a look at my code, I think there must be a way to optimize the foreach piece of code?
I have a database with Artists, each artist has multiple songTitles (called Titles), and each Title can have multiple Meanings.
Artist [1..*] Title [1..*] Meaning [0..*]
I want to find the count of Meanings, per Title, for an Artist, and return it as a new ViewModel List.
public class TitleVM
{
public int TitleID { get; set; }
public int MeaningCount { get; set; }
}
public List<TitleVM> GetTitlesByArtistID(int artistID)
{
//find the artist by ID
var titles = context.Titles.Where(x => x.ArtistID == artistID);
//create new VMList to be returned
var titleVMList = new List<TitleVM>();
//loop through each title,
foreach (var item in titles)
{
//find the number of meanings,
var count = 0;
if (item.Meanings != null && item.Meanings.Count > 0)
{
count = item.Meanings.Count();
}
// and map it to VM, add to list
titleVMList.Add(new TitleVM
{
TitleID = TitleID,
MeaningCount = count
});
}
return titleVMList;
}
I thought mapping it would be easiest, but have no idea how to map a viewmodel with lists in this way.
In my project I use Omu.ValueInjecter for mapping basic models, because Automapper needs full trust to run, and my host doesn't allow it.
Let me know if more information is needed.
Ok I read that its better to do an .AddRange then adding the the item with .Add each time.
I got my code down to the below:
public int CountMeanings(IEnumerable<Meaning> meanings)
{
if (meanings != null && meanings.Count() > 0)
return meanings.Count();
return 0;
}
public List<TitleVM> GetTitlesByArtistID(int artistID)
{
var titleVMList = new List<TitleVM>();
var titles = context.Titles.Where(x => x.ArtistID == artistID).AsEnumerable();
titleVMList.AddRange(titles.Select(item => new TitleVM {
TitleID = item.TitleID,
MeaningCount = CountMeanings(item.Meanings)
}));
return titleVMList;
}

Passing viewModel when redirecting in ASP.NET MVC 4

I have below statement:
return Redirect(this.Request.UrlReferrer.AbsolutePath);
this redirects to the caller view. It is working ok, but now I need to return a view model when redirecting, something like this (it's wrong):
return Redirect(this.Request.UrlReferrer.AbsolutePath(item));
So how can I achieve this?
I want to do this because I have a jqrid in which one of its columns offers some actions, edit and delete the row. So if user clicks on edit, i retrieve some data from the id passed to the database. Then once I get this data, I populate a view model in order to update some textboxes in the view, so I need to pass the view model when redirecting.
Below my code in the controller:
public ActionResult Edit(int id)
{
ItemViewModel item = new ItemViewModel();
using (DBContext context = new DBContext())
{
Items itemToModify = context.Items.Single(i=> i.ItemId == id);
item.Desc = itemToModify.Desc;
item.Name = itemToModify.Name;
}
return Redirect(this.Request.UrlReferrer.AbsolutePath, item); <-- how to do this
}
You can use TempData like
In your controller
public ActionResult Action1()
{
ItemViewModel item = new ItemViewModel();
TempData["item"] = item;
return Redirect("Action2");
}
public ActionResult Action2()
{
ItemViewModel item = (ItemViewModel)TempData["item"];
//Your Code
}

Lambda expressions - set the value of one property in a collection of objects based on the value of another property in the collection

I'm new to lambda expressions and looking to leverage the syntax to set the value of one property in a collection based on another value in a collection
Typically I would do a loop:
class Item
{
public string Name { get; set; }
public string Value { get; set; }
}
void Run()
{
Item item1 = new Item { Name = "name1" };
Item item2 = new Item { Name = "name2" };
Item item3 = new Item { Name = "name3" };
Collection<Item> items = new Collection<Item>() { item1, item2, item3 };
// This is what I want to simplify.
for (int i = 0; i < items.Count; i++)
{
if (items[i].Name == "name2")
{
// Set the value.
items[i].Value = "value2";
}
}
}
LINQ is generally more useful for selecting data than for modifying data. However, you could write something like this:
foreach(var item in items.Where(it => it.Name == "name2"))
item.Value = "value2";
This first selects items that need to be modified and then modifies all of them using a standard imperative loop. You can replace the foreach loop with ForAll method that's available for lists, but I don't think this gives you any advantage:
items.Where(it => it.Name == "name2").ToList()
.ForEach(it => it.Value = "value2");
Note that you need to add ToList in the middle, because ForEach is a .NET 2.0 feature that's available only for List<T> type - not for all IEnumerable<T> types (as other LINQ methods). If you like this approach, you can implement ForEach for IEnuerable<T>:
public static void ForEach<T>(this IEnumerable<T> en, Action<T> f) {
foreach(var a in en) f(a);
}
// Then you can omit the `ToList` conversion in the middle
items.Where(it => it.Name == "name2")
.ForEach(it => it.Value = "value2");
Anyway, I'd prefer foreach loop, because that also makes it clear that you're doing some mutation - and it is useful to see this fact easily in the code.