model-binding

Post checked checkboxes to controller action without using HTML helper like Html.CheckboxList

假如想象 提交于 2019-11-28 09:14:32
I have a list of items and I would like to delete items that are checked in a list of checkboxes. I can't use something like CheckboxList since I'm using Grid.Mvc to display my lines. That is why I create checkboxes in each line with column.add("<input type="checkbox".....>); . Every checkbox has its own ID: <input type="checkbox" id="3"> <input type="checkbox" id="4">... I would like to know how to pass all checked checkbox IDs to the controller (from there I will perform delete operations). How can I post an array of checked IDs from my form to my controller action with one button press?

JavaScriptSerializer and ASP.Net MVC model binding yield different results

那年仲夏 提交于 2019-11-28 08:43:10
I'm seeing a JSON deserialization problem which I can not explain or fix. Code public class Model { public List<ItemModel> items { get; set; } } public class ItemModel { public int sid { get; set; } public string name { get; set; } public DataModel data { get; set; } public List<ItemModel> items { get; set; } } public class DataModel { public double? d1 { get; set; } public double? d2 { get; set; } public double? d3 { get; set; } } public ActionResult Save(int id, Model model) { } Data {'items':[{'sid':3157,'name':'a name','items':[{'sid':3158,'name':'child name','data':{'d1':2,'d2':null,'d3'

ASP.NET MVC 2 - Binding To Abstract Model

我与影子孤独终老i 提交于 2019-11-28 08:34:10
If i have the following strongly-typed view: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<XXX.DomainModel.Core.Locations.Location>" %> Where Location is an abstract class. And i have the following Controller, which accepts a strongly-typed Model via a POST: [HttpPost] public ActionResult Index(Location model) I get a runtime error stating "Cannot Create Abstract Class Which of course makes sense. However - i'm not sure what the best solution is here. I have many concrete types (around 8), and this is a view where you can only

How do you exclude properties from binding when calling UpdateModel()?

做~自己de王妃 提交于 2019-11-28 07:02:02
问题 I have a view model sent to the edit action of my controller. The ViewModel contains references to EntityObjects. (yea i'm fine with it and don't need to want to duplicate all the entities properties in the viewmodel). I instantiate the view model and then call UpdateModel. I get an error that a property is "null" which is fine since it is a related model. I am trying to exclude the property from being bound during model binding. On debugging it I see in the entity where the model binder is

Custom Model Binder inheriting from DefaultModelBinder

感情迁移 提交于 2019-11-28 05:58:42
I'm attempting to build a custom model binder for MVC 4 that will inherit from DefaultModelBinder . I'd like it to intercept any interfaces at any binding level and attempt to load the desired type from a hidden field called AssemblyQualifiedName . Here's what I have so far (simplified): public class MyWebApplication : System.Web.HttpApplication { protected void Application_Start() { ModelBinders.Binders.DefaultBinder = new InterfaceModelBinder(); } } public class InterfaceModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,

Model binding in controller when form is posted - why to use view model instead of class from domain model?

巧了我就是萌 提交于 2019-11-28 05:53:03
问题 I'm still reasonably new to ASP.NET MVC 3. I have come across view models and their use for passing data from a controller to the view. In my recent question on model binding two experts suggested that I should use view models for model binding as well. This is something I haven't come across before. But both guys have assured me that it is best practise. Could someone maybe shed some light on the reasons why view models are more suitable for model binding? Here is an example situation: I

MVC3 - Viewmodel with list of complex types

懵懂的女人 提交于 2019-11-28 05:27:27
Apologies if this has been asked before; there are a million ways to phrase it so searching for an answer has proved difficult. I have a viewmodel with the following properties: public class AssignSoftwareLicenseViewModel { public int LicenseId { get; set; } public ICollection<SelectableDeviceViewModel> Devices { get; set; } } A simplified version of SelectableDeviceViewModel would be this: public class SelectableDeviceViewModel { public int DeviceInstanceId { get; set; } public bool IsSelected { get; set; } public string Name { get; set; } } In my View, I am attempting to display a list of

MVC4 bind model to ICollection or List in partial

有些话、适合烂在心里 提交于 2019-11-28 04:43:13
问题 Given a Model public class Task { public int TaskId { get; set; } public string Title { get; set; } public ICollection<SomeData> Information { get; set; } } where public class SomeData { public int SomeDataId { get; set; } public string Description { get; set; } } I have a view @model myProject.Models.Task <div> @Html.LabelFor(model => model.Title) </div> <table> @Html.Partial("_InformationEdit", Model.Information.ToList(), new ViewDataDictionary(Html.ViewDataContainer.ViewData) {

Model binding comma separated query string parameter

别说谁变了你拦得住时间么 提交于 2019-11-28 04:33:04
How can I bind query string parameter that is comma separated value http://localhost/Action?ids=4783,5063,5305 to a controller action expecting a list? public ActionResult Action(List<long> ids) { return View(); } Note! ids in the controller action must a list (or something IEnumerable based), so string ids is not accepted as an answer because these parameters are passed to many actions and parsing string to an array would add unwanted noise. Here's my improved version of Nathan Taylor’s solution used in archil's answer. Nathan’s binder could only bind sub-properties of complex models, while

How does a multiple select list work with model binding in ASP.NET MVC?

可紊 提交于 2019-11-28 04:18:17
If you have a select list set to multiple in ASP.NET MVC, how does the modelbinding work? What does it return for your selected items, an array? <SELECT NAME="toppings" MULTIPLE SIZE=5> <option value="mushrooms">mushrooms</option> <option value="greenpeppers">green peppers</option> <option value="onions">onions</option> <option value="tomatoes">tomatoes</option> <option value="olives">olives</option> </SELECT> Sam Wessel Yes, by default a multiselectlist will post through an array of the selected values. This article has further information, including how to use strongly-typed views with a