Because your view is strongly typed to the view model your form fields will look like this.
Up vote 1 down vote favorite share g+ share fb share tw.
I have a domain model and a view model as follows: Domain Model: namespace MvcApplication1. Models { public enum Sex { Male, Female }; public class Person { public int Id { get; set; } public string Name { get; set; } Required(ErrorMessage="Please select either Female or Male. ") public Sex?
Sex { get; set; } } } View Model: namespace MvcApplication1. ViewModels { public class HomeCreateVM { public HomeCreateVM() { } public HomeCreateVM(Person p) { Person = p; SelectList = p.Sex.GetSelectList(); } public Person Person { get; set; } public SelectList SelectList { get; set; } } } The auxiliary extension method is defined as follows: namespace MvcApplication1. Models { public static class Utilities { public static SelectList GetSelectList(this XXX?
Obj) where XXX : struct { var values = from XXX x in Enum. GetValues(typeof(XXX)) select new { Text = x.ToString(), Value = x }; return new SelectList(values, "Value", "Text", obj); } } } Controller: public ActionResult Create() { var p = new Person(); return View(new HomeCreateVM(p)); } HttpPost public ActionResult Create(Person hc)// the source of problem! { if (ModelState.
IsValid)//always false! { TempData"status" = hc; return RedirectToAction("Confirm"); } else return View(new HomeCreateVM(hc)); } HomeCreateVM. Cshtml: @model MvcApplication1.ViewModels.
HomeCreateVM Name: @Html. EditorFor(model => model.Person. Name) Sex: @Html.
DropDownListFor(model => model.Person. Sex, Model. SelectList, "--Select--") Create View: @model MvcApplication1.ViewModels.
HomeCreateVM @{ ViewBag. Title = "Create"; } Create @using (Html.BeginForm()) { @Html. ValidationSummary(true) HomeCreateVM @Html.EditorForModel() } @Html.
ActionLink("Back to List", "Index") Question: There is no problem if the POST Create action method accepts a HomeCreateVM object as the argument. However, if I change the POST Create action method argument from HomeCreateVM to Person (as shown in the code above), ModelState. IsValid always returns false.
The question is: "Is it possible to pass a ViewModel object to a Create view but only accept a DomainModel object from a POST Create action method? " asp. Net-mvc asp.
Net-mvc-3 link|improve this question edited Feb 6 '11 at 10:55 asked Feb 6 '11 at 10:49LaTeX400111 96% accept rate.
Because your view is strongly typed to the view model your form fields will look like this: and if you want to bind correctly you need to specify the prefix: HttpPost public ActionResult Create(Bind(Prefix = "Person")Person hc) { ... }.
– LaTeX Feb 6 '11 at 11:14 2 @user596314, I understand your concern but unfortunately it is not possible. Only constant values can be passed to attributes. – Darin Dimitrov Feb 6 '11 at 11:16 OK.
Thanks again for the solution. – LaTeX Feb 6 '11 at 11:30.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.