model-binding

Model Binding to Enums in ASP.NET MVC 3

六眼飞鱼酱① 提交于 2019-11-26 12:00:33
问题 I have a method in my controller that accepts an object as an argument and returns a JsonResult. One of the properties on this object is an enum with three possible values. I assumed that when the client passed in an int for that property it would populate the enum, but it doesn\'t, it defaults to 0 and the enum is set to the first of it\'s possible selections. Any suggestions? 回答1: NOTE: This has been resolved in MVC 4. If upgrading to MVC 4 is a viable option for your project then that is

Bind Angularjs to newly created html element dynamically

半世苍凉 提交于 2019-11-26 11:07:11
问题 I have a tab page with multiple tabs that once clicked on call a service to return some data. Some of that data returns html forms and are very random. I want to collect those values that are entered and send the data via a service back to the server. The problem I have is that I can\'t get the data from the input elements in the html I\'m creating dynamically. I\'ve created a Plunker to show what the issue is. Note that the html value can change at any time so hard-coding the html won\'t

Custom model binder for a property

断了今生、忘了曾经 提交于 2019-11-26 10:57:54
问题 I have the following controller action: [HttpPost] public ViewResult DoSomething(MyModel model) { // do something return View(); } Where MyModel looks like this: public class MyModel { public string PropertyA {get; set;} public IList<int> PropertyB {get; set;} } So DefaultModelBinder should bind this without a problem. The only thing is that I want to use special/custom binder for binding PropertyB and I also want to reuse this binder. So I thought that solution would be to put a ModelBinder

How to pass IEnumerable list to controller in MVC including checkbox state?

浪子不回头ぞ 提交于 2019-11-26 10:28:57
I have an mvc application in which I am using a model like this: public class BlockedIPViewModel { public string IP { get; set; } public int ID { get; set; } public bool Checked { get; set; } } Now I have a View to bind a list like this: @model IEnumerable<OnlineLotto.Web.Models.BlockedIPViewModel> @using (Html.BeginForm()) { @Html.AntiForgeryToken() } @foreach (var item in Model) { <tr> <td> @Html.HiddenFor(x => item.IP) @Html.CheckBoxFor(x => item.Checked) </td> <td> @Html.DisplayFor(modelItem => item.IP) </td> </tr> } <div> <input type="submit" value="Unblock IPs" /> </div> Now I have a

error with decimal in mvc3 - the value is not valid for field

霸气de小男生 提交于 2019-11-26 09:47:09
问题 I\'m following [Getting started with ASP.NET MVC 3][1]. And I can\'t add/edit with value of Price = 9.99 or 9,99. It said: \"The value \'9.99\' is not valid for Price.\" and \"The field Price must be a number.\" How to fix this? Model: public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDbContext : DbContext { public DbSet<Movie>

ASP.NET MVC 3 JSONP: Does this work with JsonValueProviderFactory?

南笙酒味 提交于 2019-11-26 09:40:21
问题 Phil Haack has an excellent blog post on how to use JSON, data binding, and data validation. Enter the browser\'s \"same origin policy security restriction.\" and JSONP where you use $.getJSON() to retrieve the content. Is there a built in MVC 3 way to do this, or do I need to follow the advice of posts like this? Can you post content? I ask because my colleague implemented a JsonPfilterAttribute among other things to make this work. It\'s obviously preferred to avoid that if something

How to make ASP.Net MVC model binder treat incoming date as UTC?

女生的网名这么多〃 提交于 2019-11-26 09:33:43
问题 I\'m posting an object to an MVC controller. The object contains a field called StartDt and on the client it is a javascript Date object in local time. When I call JSON.stringify on the object and POST it to the server using jQuery\'s ajax method I can see in Firebug that what\'s being sent to the server is an ISO string like \"1900-12-31T13:00:00.000Z\" which I believe should be the local time in UTC format. When I look at the DateTime field in my controller though, it looks like its back to

How does MVC 4 List Model Binding work?

吃可爱长大的小学妹 提交于 2019-11-26 05:58:21
问题 If I want a set of inputs in a form to bind to a List in MVC 4, I know that the following naming convention for input name attributes will work: <input name=\"[0].Id\" type=\"text\" /> <input name=\"[1].Id\" type=\"text\" /> <input name=\"[2].Id\" type=\"text\" /> But I am curious about how forgiving the model binder is. For example, what about the following: <input name=\"[0].Id\" type=\"text\" /> <input name=\"[3].Id\" type=\"text\" /> <input name=\"[8].Id\" type=\"text\" /> How would the

MVC 3 Model Binding a Sub Type (Abstract Class or Interface)

流过昼夜 提交于 2019-11-26 05:23:38
问题 Say I have a Product model, the Product model has a property of ProductSubType (abstract) and we have two concrete implementations Shirt and Pants. Here is the source: public class Product { public int Id { get; set; } [Required] public string Name { get; set; } [Required] public decimal? Price { get; set; } [Required] public int? ProductType { get; set; } public ProductTypeBase SubProduct { get; set; } } public abstract class ProductTypeBase { } public class Shirt : ProductTypeBase {

Remote Validation for LIST of MODELs

隐身守侯 提交于 2019-11-26 03:59:55
问题 I used the following tutorial: http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.aspx And everything seemed fine, but in my case, string Username always comes back null. After tonnes of research, I found everyone discovered BIND Prefixes. That would be great in many circumstances, but not this one. I should note all properties and names line up, however in my for loop, the EditorFor creates a [i].Username field and this doesn\'t map to any model property. QUESTION: I think I want to