model-binding

How to handle MVC model binding prefix with same form repeated for each row of a collection?

廉价感情. 提交于 2019-12-04 12:37:27
My main view model has a collection of ChildViewModel. In the view, I loop over the collection and call EditorFor(), like so: @for (int i = 0; i < Model.Children.Count; i++) { @Html.EditorFor(m => m.Child[i]); } The editor template looks like: @model ChildModel @using (Html.BeginForm("EditChild", "MyController")) { @Html.HiddenFor(m => m.ChildId) @Html.TextBoxFor(m => m.ChildName) } This will generate markup where each children is in a separate form, and each such form will have an input control with name like Child[0].ChildName. I use a separate form for each children, as the children will be

Hydrating ViewModels in ASP.NET MVC

我们两清 提交于 2019-12-04 11:19:11
I have a page that is made up of many user controls. The view model for this page is rather complex. public class ComplexViewModel { public ObjectA ObjectAProperty { get; set; } public List<Things> ListOfThings { get; set; } public List<ThingCategories> ListOfThingCategories { get; set; } public List<ThingTypes> ListOfThingTypes { get; set; } public List<ThingOptions> ListOfThingOptions { get; set; } public int ChosenThingCategoryId { get; set; } public int ChosenThingTypeId { get; set; } public int ChosenThingOptionId { get; set; } public OtherObject ObjectData { get; set; } } This page also

What, exactly, does a modelbinder do? How to use it effectively?

大城市里の小女人 提交于 2019-12-04 10:02:23
I was researching something and came across this blog post at buildstarted.com about model binders. It actually works pretty darn well for my purposes but I am not sure exactly whats going on behind the scenes. What I did was create a custom ModelBinder called USerModelBinder : public class UserModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult value = bindingContext.ValueProvider.GetValue("id"); MyEntities db = new MyEntities(); User user = db.Users.SingleOrDefault(u => u.UserName == value

If I need to retrieve an object from a custom model binder should the binder interact with the service layer, the repository layer, or …?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 09:30:45
If I have a class similar to this: public class Person { public string firstName { get; set; } public string lastName { get; set; } public Pet myPet { get; set; } } When I create a custom model binder, the Post from my form will not be sending in a Pet, it would send in data like this: firstName: "myFirstName" lastName: "myLastName" myPet: "myPetsName" Since the Pet's name is passed in, and not the actual Pet object, the Pet object needs to be retrieved from within the model binder. My question is, should the model binder be interacting with the Service Layer, the Repository Layer, or should

Bind Checkboxes to int array/enumerable in MVC

耗尽温柔 提交于 2019-12-04 09:02:53
问题 @Html.CheckBox("orderNumbers", new { value = 1 }) @Html.CheckBox("orderNumbers", new { value = 2 }) @Html.CheckBox("orderNumbers", new { value = 3 }) @Html.CheckBox("orderNumbers", new { value = 4 }) @Html.CheckBox("orderNumbers", new { value = 5 }) [HttpPost] public ActionResult MarkAsCompleted(IEnumerable<int> orderNumbers) { } [HttpPost] public ActionResult MarkAsCompleted(IEnumerable<string> orderNumbers) { } If I use the first signature in my action method, I get an empty IEnumerable .

How to do Model Binding with Jquery Ajax

你离开我真会死。 提交于 2019-12-04 07:54:23
I'd like to use model binding to keep my controllers looking cleaner, you can see how much nicer it is using model binding: public ActionResult Create(Person personToCreate) { //Create person here } vs public ActionResult Create(string firstName, string lastName, string address, string phoneNum, string email, string postalCode, string city, string province, string country) { //Create person here } When doing model binding, we can just use a form with the correct names in the Html.TextBox("") What about jquery? How can I make sure that when I do a $.post(url, data, callback, dataType) or a $

GridView SelectMethod Issue With WebPart Sharepoint 2013

家住魔仙堡 提交于 2019-12-04 06:39:09
问题 Hi every one , I have a problem with a gridview in webpart sharepoint 2013 ,I use model bind with the gridview and I have assigned a select method to the gridview when I deployed the webpart it raise the following error A public method with the name was either not found or there were multiple methods with the same name on the type x.master thanks alot 回答1: I have found the solution , you need to register this code within the CallingDataMethod GridView event : protected void GridView1

Using one Partial View Multiple times on the same Parent View

有些话、适合烂在心里 提交于 2019-12-04 05:40:37
I am using MVC3 razor. I have a scenario where I have to use a partial view multiple times on the same parent view. The problem I am having is that when the Parent View gets rendered, it generates same names and ids of the input controls within those partial views. Since my partial views are binded to different models, when the view is posted back on "Save" it crashes. Any idea how can i make the control id/names unique, probably some how prefix them ? Awaiting Nabeel Personally I prefer using editor templates, as they take care of this. For example you could have the following view model:

MVC2 RTM - model binding complex objects using Entity Framework

别来无恙 提交于 2019-12-04 05:19:37
问题 I am new to MVC, and am really struggling with what I seems like it should be a very common scenario. I'm using MVC2 RTM, and the Entity Framework for my model objects. What I have working: An edit view for a parent object that contains a collection of child objects. The form displays all the editable fields for the parent, and iterates through and displays all editable fields for all the associated child objects (in the same view). I am able to successfully handle the edit action in my

How to check for a property attribute inside a custom model binder

非 Y 不嫁゛ 提交于 2019-12-04 05:09:45
I want to enforce that all dates in my system are valid and not in the future, so I enforce them inside the custom model binder: class DateTimeModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); try { var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture); // Here I want to ask first if the property has the FutureDateAttribute if ((DateTime)date > DateTime.Today) { bindingContext.ModelState.AddModelError(bindingContext