model-binding

ASP.NET MVC model binding foreign key relationship

随声附和 提交于 2019-11-27 15:14:11
问题 Is it possible to bind a foreign key relationship on my model to a form input? Say I have a one-to-many relationship between Car and Manufacturer . I want to have a form for updating Car that includes a select input for setting Manufacturer . I was hoping to be able to do this using the built-in model binding, but I'm starting to think I'll have to do it myself. My action method signature looks like this: public JsonResult Save(int id, [Bind(Include="Name, Description, Manufacturer")]Car car)

Steve Sanderson's BeginCollectionItem helper won't bind correctly

六月ゝ 毕业季﹏ 提交于 2019-11-27 14:52:17
I am using Steve Sanderson's BeginCollectionItem helper and ran into a problem. I have a form that has an option to add unlimited reward fields. I am using his helper since it solved this problem with how to keep generating the fields and not have to worry about how to bind it when the form gets submitted. I have in this same form some checkboxes that there is an unknown amount. The difference with this one versus the rewards is the unknown amount will become known after a database call and will be known by the time the code gets to the view. So my code looks like this public class FrmVm {

Dynamic list of checkboxes and model binding

感情迁移 提交于 2019-11-27 14:09:26
问题 I'm trying to create a view that contains a list of checkboxes that is dynamically created from a database, and then retrieve the list of selected ones when the form is posted back. My EF model contains a class: public class ItemIWouldLikeACheckboxFor { public int Id { get; set; } public string Description { get; set; } } I have a view model that contains a list of these: public class PageViewModel { // various other properties public List<ItemIWouldLikeACheckboxFor> checkboxList { get; set;

bind attribute include and exclude property with complex type nested objects

痞子三分冷 提交于 2019-11-27 13:59:47
问题 Ok, this is weird. I cannot use BindAttribute 's Include and Exclude properties with complex type nested objects on ASP.NET MVC. Here is what I did: Model: public class FooViewModel { public Enquiry Enquiry { get; set; } } public class Enquiry { public int EnquiryId { get; set; } public string Latitude { get; set; } } HTTP POST action: [ActionName("Foo"), HttpPost] public ActionResult Foo_post( [Bind(Include = "Enquiry.EnquiryId")] FooViewModel foo) { return View(foo); } View: @using (Html

MVC 4 ignores DefaultModelBinder.ResourceClassKey

我是研究僧i 提交于 2019-11-27 11:57:04
Adding a resource file to App_GlobalResources with a PropertyValueRequired key and changing DefaultModelBinder.ResourceClassKey to the file name has no effect on MVC 4. The string The {0} field is required is never changed. I don't want to set the resource class type and key on every required field. Am I missing something? Edit: I've made a small modification on Darin Dimitrov's code to keep Required customizations working: public class MyRequiredAttributeAdapter : RequiredAttributeAdapter { public MyRequiredAttributeAdapter( ModelMetadata metadata, ControllerContext context, RequiredAttribute

ASP.net MVC - Model binding excludes class fields?

纵饮孤独 提交于 2019-11-27 09:44:16
In a recent project - i've hit an unexpected snag. A class with simple public fields (note not properties) doesn't seem to want to play nice with the ASP.net MVC 3.0 model binder. Is this by design? besides changing the fields to properties - any options here? update The reason for the simple fields (instead of properties) is because I'm working with a shared library between MVC and a Script Sharp project. Script sharp supports properties - but it becomes a mess with javascript in the view (using knockout.js) So despite what i'd love (which is to just use properties) i'm using public fields in

Model binding with nested child models and PartialViews in ASP.NET MVC

左心房为你撑大大i 提交于 2019-11-27 09:06:59
I have the following types and classes: namespace MVC.Models public class Page { public EditableContent Content {get; set; } } public class EditableContent { public TemplateSection SidebarLeft {get; set; } public TemplateSection SidebarRight {get; set; } } I want to edit the Page instance in my Edit.aspx View. Because EditableContent is also attached to other models, I have a PartialView called ContentEditor.ascx that is strongly typed and takes an instance of EditableContent and renders it. The rendering part all works fine, but when I post - everything inside my ContentEditor is not binded -

Calling UpdateModel with a collection of complex data types reset all non-bound values?

五迷三道 提交于 2019-11-27 08:29:47
I'm not sure if this is a bug in the DefaultModelBinder class or what. But UpdateModel usually doesn't change any values of the model except the ones it found a match for. Take a look at the following: [AcceptVerbs(HttpVerbs.Post)] public ViewResult Edit(List<int> Ids) { // Load list of persons from the database List<Person> people = GetFromDatabase(Ids); // shouldn't this update only the Name & Age properties of each Person object // in the collection and leave the rest of the properties (e.g. Id, Address) // with their original value (whatever they were when retrieved from the db)

ASP.NET MVC Model Binder for Generic Type

萝らか妹 提交于 2019-11-27 07:51:30
Is it possible to create a model binder for a generic type? For example, if I have a type public class MyType<T> Is there any way to create a custom model binder that will work for any type of MyType? Thanks, Nathan Create a modelbinder, override BindModel, check the type and do what you need to do public class MyModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (HasGenericTypeBase(bindingContext.ModelType, typeof(MyType<>)) { // do your thing } return base.BindModel(controllerContext,

How ASP.NET MVC: How can I bind a property of type List<T>?

眉间皱痕 提交于 2019-11-27 07:39:56
问题 Let's say I've the bellow model public class UserInformation { public List<UserInRole> RolesForUser { get; set; } //Other properties omitted... } public class UserInRole { public string RoleName { get; set; } public bool InRole { get; set; } } On my page I have something like <%using(Html.BeginForm()){%> .../... <%for(int i =0; i<Model.InRoles.Cout; i++%> <p><%: Html.CheckBox(Model.Roles[i].RoleName, Model.Roles[i].InRole)%></p> <%}%> The idea is to be able to check/uncheck the checkbox so