How do I edit an IEnumerable<T> with ASP.NET MVC 3? - asp.net-mvc

Given the following types
public class SomeValue
{
public int Id { get; set; }
public int Value { get; set; }
}
public class SomeModel
{
public string SomeProp1 { get; set; }
public string SomeProp2 { get; set; }
public IEnumerable<SomeValue> MyData { get; set; }
}
I want to create an edit form for the type SomeModel which would contain the usual text fields for SomeProp1 and SomeProp2 and then a table containing a text field for each SomeValue in the SomeModel.MyData collection.
How is this done? How do the values get bound back to the model?
I currently have a form displaying a text field for each value but they all have the same name and same Id. This is obviously not valid HTML and will prevent MVC from mapping the values back.

You would do it using Editor Templates. This way the framework will take care of everything (from properly naming the input fields to properly binding the values back in the post action).
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
// In the GET action populate your model somehow
// and render the form so that the user can edit it
var model = new SomeModel
{
SomeProp1 = "prop1",
SomeProp2 = "prop1",
MyData = new[]
{
new SomeValue { Id = 1, Value = 123 },
new SomeValue { Id = 2, Value = 456 },
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(SomeModel model)
{
// Here the model will be properly bound
// with the values that the user modified
// in the form so you could perform some action
return View(model);
}
}
View (~/Views/Home/Index.aspx):
<% using (Html.BeginForm()) { %>
Prop1: <%= Html.TextBoxFor(x => x.SomeProp1) %><br/>
Prop2: <%= Html.TextBoxFor(x => x.SomeProp2) %><br/>
<%= Html.EditorFor(x => x.MyData) %><br/>
<input type="submit" value="OK" />
<% } %>
And finally the Editor Template (~/Views/Home/EditorTemplates/SomeValue.ascx) which will be automatically invoked for each element of the MyData collection:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.SomeValue>" %>
<div>
<%= Html.TextBoxFor(x => x.Id) %>
<%= Html.TextBoxFor(x => x.Value) %>
</div>

IList implements IEnumerable so you could modify your model like so:
public class SomeModel {
public string SomeProp1 { get; set; }
public string SomeProp2 { get; set; }
public IList<SomeValue> MyData { get; set; }
}
You can use the IModelBinder interface to create a binder for your specific model. There are a couple ways to do it. You can create an EditorFor cshtml for the model which will loop through your SomeValue list and output appropriate ids and what not. Then, in your ModelBinder implementation your would then read through your ids and bind them appropriately. I can post a working sample in a while.

Related

MVC4 DropDownListFor Object reference not set to an instance of an object

I'm about to go mad trying to figure this out. I'm newer to MVC but have become pretty comfortable with it.
I am trying to place a Dropdown List on a View but keep getting "Object reference not set to an instance of an object" on the line that is calling the DropDownListFor. Here's what I have:
Model
public class UserModel
{
public class DietObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public IEnumerable<DietObject> DietOptions = new List<DietObject>
{
new DietObject { Id = 0, Name = "Meat" },
new DietObject { Id = 1, Name = "Vegetarian" }
};
[Required]
[StringLength(15)]
[Display(Name = "Diet:")]
public string Diet { get; set; }
}
Controller
[HttpGet]
public ActionResult Registration()
{
return View();
}
View
<div class="fHeader">
<%: Html.LabelFor(m => m.Diet) %>
</div>
<div class="fText">
<%: Html.DropDownListFor(m => m.Diet, new SelectList(Model.DietOptions, "Id", "Name", Model.DietOptions.First().Id))%>
<%: Html.ValidationMessageFor(m => m.Diet)%>
</div>
Error
During the load of the page, it errors on this line:
<%: Html.DropDownListFor(m => m.Diet, new SelectList(Model.DietOptions, "Id", "Name", Model.DietOptions.First().Id))%>
Someone put me out of my misery. Much appreciated.
If you want to directly access the Model property in your view (in this case writing Model.DietOptions) then you need to pass in a model instance when calling View in your controller:
[HttpGet]
public ActionResult Registration()
{
return View(new UserModel());
}
Otherwise the Model will be null and you will get a nullrefence excpetion.

ASP .NET MVC4 Adding new items to view and model binding

I create a website for my wife. She's a teacher and she would like to have a possibility to create exercises for their students. The case is that she would like to create for instance the following exercise:
Exercise 1: Fill the sentence using a correct word:
My wife is 30 ............. old
I live in this city for 30 .........
I have the following model:
public class Exercise
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ExerciseType Type { get; set; }
public DifficulityLevel DifficulityLevel { get; set; }
public List<ExerciseItem> Items { get; set; }
public DateTime TimeOfCreation { get; set; }
public DateTime TimeOfModification { get; set; }
}
public class ExerciseItem
{
[Key]
public Guid Id { get; set; }
public string Content { get; set; }
public List<ExerciseItemOption> Options { get; set; }
public ExerciseItemOption CorrectSelection { get; set; }
}
I creates a View for my Exercise. I can fill in the basic properties like Name, Description, Difficulity Level and Type. Then I would like to create a button "Add exercise item". When clicked, a partial view (or something else) should be added dynamically where new ExerciseItem can be provided.
I've tried to following:
I've added a button
#Ajax.ActionLink("Add exercise item",
"AddExerciseItem",
"Exercise", new AjaxOptions() { HttpMethod="GET", InsertionMode = InsertionMode.InsertBefore, UpdateTargetId="ExerciseItems"})
and the appropriate div:
<div id="ExerciseItems"></div>
My action method looks as follows:
public ActionResult AddExerciseItem()
{
return PartialView("ExerciseItem", new ExerciseItem());
}
and the partial view:
#model ElangWeb.Models.ExerciseItem
<fieldset>
<legend>ExerciseItem</legend>
#Html.HiddenFor(model => model.Id)
<div class="editor-label">
#Html.DisplayNameFor(model => model.Content)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Content, new { style = "width:200px" })
</div>
</fieldset>
It works fine. However when I click button for creating a whole exercise, I do not have ExerciseItem collection in my model:
public ActionResult Create(Exercise exercise)
{
using (PersistanceManager pm = new PersistanceManager())
{
exercise.Id = Guid.NewGuid();
exercise.TimeOfCreation = DateTime.Now;
exercise.TimeOfModification = DateTime.Now;
pm.ExcerciseRepository.Add(exercise);
}
return RedirectToAction("Index");
}
How should I change the code in order to bind my list of added ExerciseItem objects to my model Exercise?
Check out this article about model binding. You basically need to create special names for the exercise items so that they get bound correctly.
e.g. partial:
#model ElangWeb.Models.ExerciseItem
<fieldset>
<legend>ExerciseItem</legend>
<label>content</label>
<input type="hidden" name="ExcersiseItem.Index" value="SomeUniqueValueForThisItem" />
<input type="text" name="ExcersiseItem[SomeUniqueValueForThisItem].Name" value="#Model.Content" />
</fieldset>
You can also look at my answer to this question MVC3 Non-Sequential Indices and DefaultModelBinder. Thanks Yarx for finding it, I was actually trying to find it :)

How to pass an entire ViewModel back to the controller

I have a ViewModel that contains two objects:
public class LookUpViewModel
{
public Searchable Searchable { get; set; }
public AddToSearchable AddToSearchable { get; set; }
}
The two contained models look something like this:
public class Searchable
{
[Key]
public int SearchableId { get; set; }
public virtual ICollection<AddToSearchable> AddedData { get; set; }
}
public class AddToSearchable
{
[Key]
public int AddToSearchableId { get; set;}
[Required]
public int SearchableId { get; set; }
[Required]
public String Data { get; set; }
[Required]
public virtual Searchable Searchable { get; set; }
}
I have a view that uses my LookUpViewModel and receives input to search for a SearchableId. If the Searchable object is found, a LookUpViewModel object is created and passed to the View. The view then displays editor fields for AddToSearchable.Data. Once submitted, I want the LookUpViewModel to be passed to an action method to handle all the back-end code. The only problem is, the LookUpViewModel passed to my action method contains a null reference to Searchable and a valid reference to AddToSearchable.. i.e. I'm missing half of my data.
Here's an example of what my view looks like:
#model HearingAidTrackingSystem.ViewModels.LookUpViewModel
#using (Html.BeginForm("LookUp", "Controller", "idStr", FormMethod.Post))
{
<input type="text" name="idStr" id="idStr"/>
<input type="submit" value="Search" />
}
#if (Model.Searchable != null && Model.AddToSearchable != null)
{
using (Html.BeginForm("AddMyStuff", "Controller"))
{
Html.HiddenFor(model => model.Searchable.SearchableId);
Html.HiddenFor(model => model.Searchable.AddedData);
Html.HiddenFor(model => model.AddToSearchable.AddToSearchableId);
Html.HiddenFor(model => model.AddToSearchable.SearchableId);
Html.HiddenFor(model => model.AddToSearchable.Searchable);
<div class="editor-field">
#Html.EditorFor(model => model.AddToSearchable.Data)
#Html.ValidationMessageFor(model => model.AddToSearchable.Data);
</div>
<input type="submit" value="Submit" />
}
}
and here are my action methods:
public ActionResult LookUp(LookUpViewModel vm)
{
return View(vm);
}
[HttpPost]
public ActionResult LookUp(string idStr)
{
int id = /*code to parse string to int goes here*/;
Searchable searchable = dal.GetById(id);
LookUpViewModel vm = new LookUpViewModel { Searchable = searchable,
AddToSearchable = new AddToSearchable() };
//When breakpoint is set, vm contains valid references
return View(vm);
}
[HttpPost]
public ActionResult AddMyStuff(LookUpViewModel vm)
{
//**Problem lies here**
//Do backend stuff
}
Sorry for the lengthy post. I tried my best to keep it simple. Any suggestions you may have.. fire away.
Two methods to fix it:
You can add to do HiddenFor() for all properties of Model.Searchable.
You can use serialization to transfer your Model.Searchable into text presentation and repair it from serialized form in controller.
Update: The problem is: You need to use #Html.HiddenFor(), not Html.HiddenFor();.

Problem binding selected value to DropDownListFor inside Editor Template

Description
I have a payment page that includes a form for entering bank account information. I have encapsulated the bank account information into a Model / Editor Template. The page itself has its own View Model, which happens to contain a BankAccount property to be passed in to the Editor.
[[View Models]]
public class PaymentPageModel {
public SomeProperty1 { get; set; }
public SomeProperty2 { get; set; }
public BankAccount BankAccount { get; set; }
...
}
public class BankAccount {
public int BankAccountTypeID { get; set; }
public string BankName { get; set; }
public string ABACode { get; set; }
public string AccountNumber { get; set;}
public IEnumerable<SelectListItem> BankAccountTypes {
get { ... // Constructs the list }
}
}
[[Payment Page HTML]]
<% using (Html.BeginForm()) { %>
<%: Html.EditorFor(m => m.BankAccount) %>
... // Other miscellaneous stuff not related to the actual BankAccount
<% } %>
[[Editor Template]
...
<%: Html.DropDownListFor(m => m.BankAccountTypeID, Model.BankAccountTypes) %>
...
Problem
Initially, this worked perfectly when I was strongly-typing the Payment page directly to the BankAccount model. The dropdown list was being populated properly, and the correct value from the model was being selected.
I recently modified the page, strongly-typing it to the PaymentPageModel, which contains the BankAccount model as a property. The HTML has not been modified. The result now, is that all the HTML values in the Editor Template are being populated properly, except for the DropDownList. It is binding the list of values properly from the BankAccountTypes select list, but the selected value is NOT being bound. I have checked to make sure that the value it is supposed to be binding to IS set properly by outputting it right next to the DropDownList.
This is driving me nuts, and is making me really question the reliability of Model binding and HTML Helpers in general, especially if I am unable to combine complex view models with Editor Templates to encapsulate presentation/functionality.
Any suggestions are greatly appreciated.
If you have strongly typed the editor template to PaymentPageModel in your main view instead of:
<%: Html.EditorFor(m => m.BankAccount) %>
you could try:
<%: Html.EditorForModel() %>
and in your editor template:
<%: Html.DropDownListFor(m => m.BankAccount.BankAccountTypeID,
Model.BankAccount.BankAccountTypes) %>

How to Validate with ASP.NET MVC - Model State is Invalid When Form Submitted

I am trying to validate a form in MVC.
I add custom errors to model state and get it as invalid when form is submitted. When the view is displayed it doesn’t show validation messages nor validation summary. Can anyone please let me know what am I doing wrong or point me in the right direction if there is any other way of validating?
Edit This is ASP.NET MVC 1. Here's the code:
Following is the entity
namespace DCS.DAL.Entities
{
public class Group : IDataErrorInfo
{
public int GroupId { get; set; }
public string GroupName { get ; set; }
public string AboutText { get; set; }
public string LogoURL { get; set; }
public string FriendlyURL { get; set; }
public bool ExcludeFromFT { get; set; }
public ContactInfo ContactInfo { get; set; }
public string Error { get { return string.Empty; } }
public string this[string propName]
{
get
{
if ((propName == "GroupName") && string.IsNullOrEmpty(GroupName))
return "Please enter Group Name";
return null;
}
}
}
}
Following is the view
<%= Html.ValidationSummary("Please correct following details") %>
<% using (Html.BeginForm()) {%>
<div id="divError" Style="display:none;">
errors
<%
foreach (KeyValuePair<string, ModelState> keyValuePair in ViewData.ModelState)
{
foreach (ModelError modelError in keyValuePair.Value.Errors)
{
%>
<% Response.Write(modelError.ErrorMessage); %>
<%
}
}
%>
</div>
<fieldset>
<table>
<tr>
<td>
<label for="GroupName">Group Name:</label>
</td>
<td>
<%= Html.TextBox("GroupName", Model.GroupName) %>
<%= Html.ValidationMessage("GroupName","group") %>
</td>
Foreach loop is for testing, it does gets into the for loop but doesn’t response.write error message nor validation summary nor validation message.
Following is the controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditGroup(Group group, FormCollection collection)
{
//Group group = new Group();
bool success = false;
try
{
var contactInfo = new ContactInfo
{
ContactName = collection["ContactName"],
Email = collection["Email"],
Fax = collection["Fax"],
HeadOfficeAddress = collection["HeadOfficeAddress"],
Freephone = collection["Freephone"],
Telephone = collection["Telephone"],
Website = collection["Website"]
};
group.ContactInfo = contactInfo;
group.GroupName = collection["GroupName"];
if(string.IsNullOrEmpty(group.GroupName))
{
ModelState.AddModelError("GroupName", "Please enter group name");
}
if (!ModelState.IsValid)
{
success = groupRepository.InsertUpdateGroup(group);
return View(group);
}
}
catch
{
}
//return Json(success);
return View(group);
}
It does go into the if(!Modelstate.isvalid) loop but it doesn’t display error.
Edit 2 I can see in the Text Visualiser that the Validation Summary does have the error message, it wont display on screen though.
Thanks
You could decorate your model properties with data annotation attributes allowing you to perform some validation logic. Here's a simplified example:
Model:
public class Group
{
[Required]
public string GroupName { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Group());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(Group group)
{
// Remark: You don't need the FormCollection as argument to this action,
// leave the default model binder do the job - it will also work
// for the ContactInfo property as long as you name your text fields
// appropriately. For example Html.TextBox("ContactInfo.Email", Model.ContactInfo.Email)
return View(group);
}
}
View:
<% using (Html.BeginForm()) { %>
<label for="GroupName">Group Name:</label>
<%= Html.TextBox("GroupName", Model.GroupName) %>
<%= Html.ValidationMessage("GroupName", "group") %>
<input type="submit" value="Post" />
<% } %>
It's up to you to decide whether Data Annotations are sufficient for your case, but bear in mind that if you need to perform more advanced validation scenarios you might take a look at third party frameworks like FluentValidation and xVal.

Resources