model-binding

ASP.NET MVC 2 - Binding To Abstract Model

我的梦境 提交于 2019-11-27 02:15:39
问题 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

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

扶醉桌前 提交于 2019-11-27 01:22:46
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 already exists in MVC 3. Edit: Summary: everything works with the exception of accessing a POST variable, i.e.,

Custom Model Binder inheriting from DefaultModelBinder

倾然丶 夕夏残阳落幕 提交于 2019-11-27 01:08:19
问题 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 :

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

主宰稳场 提交于 2019-11-27 01:08:10
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 local time and not UTC. How can I fix this? I want to store the UTC version of the Date that came from the

MVC3 - Viewmodel with list of complex types

不打扰是莪最后的温柔 提交于 2019-11-27 00:57:13
问题 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

HiddenFor(x => x.Id) is being populated by the UrlParameter instead of the ViewModel

时光怂恿深爱的人放手 提交于 2019-11-26 21:37:34
问题 public ActionResult SomeAction(int Id){ //Id is set to 2 var model = //get some thing from db using Id(2); //Now model.Id is set to 9; return View(model); } ----------View---------- @Html.HiddenFor(x => x.Id) When I view source this hidden field is set to 2 not 9. How do I get it to map to the model instead of mapping to the URL routing info? P.S. I'd prefer not to rename parameters because then I lose my nice looking url's unless i change the routing info. I have done that and it does work,

AllowHtml attribute not working

与世无争的帅哥 提交于 2019-11-26 21:28:03
问题 I have a model with this property: [AllowHtml] [DisplayName("Widget for Table")] [StringLength(1000, ErrorMessage = "Maximum chars 1000")] [DataType(DataType.Html)] public object TableWidget { get; set; } And here is the create methods in controller: // // GET: /Admin/Table/Create public ActionResult Create(int id) { Season season = _seasonRepository.GetSeason(id); var table = new Table { SeasonId = season.SeasonId }; return View(table); } // // POST: /Admin/Table/Create [HttpPost] public

Model Binding With Disabled Textbox

ⅰ亾dé卋堺 提交于 2019-11-26 20:30:51
问题 I have a textbox that I am defining as <%= Html.TextBox("Username", Model.Form.Username, new { @class = "textbox", @disabled = "disabled" })%> In my action [AcceptVerbs(HttpVerbs.Post)] [ValidateAntiForgeryToken] public ActionResult EditLogin(LoginForm post) { ... return View(model); } post.Username will be blank, all other properties bind correctly, but if I change @disabled="disabled to @readonly="readonly" the username binds properly and everything works. It looks like model binding

How do I sub in JSON.NET as model binder for ASP.NET MVC controllers?

徘徊边缘 提交于 2019-11-26 20:27:22
问题 It has been decided by the ASP.NET Web API team to use the JSON.NET library for model binding JSON data. However, "normal" MVC controllers still use the inferior JsonDataContractSerializer. This causes issues with parsing dates, and is causing me much headache. See this for reference: http://www.devcurry.com/2013/04/json-dates-are-different-in-aspnet-mvc.html The author chooses to solve the issue in the Knockout layer on the client. But I would prefer to solve this by using the same JSON.NET

ASP.NET MVC model binding an IList<> parameter

走远了吗. 提交于 2019-11-26 19:59:58
问题 [I solved this myself, see my answer for cause] I'm having trouble getting form values for a IList<> argument in a controller method set properly. My controller class looks like this: public class ShoppingBasketController : Controller { public ActionResult Index() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(IList<ShoppingBasketItem> items) { Session["basket"] = items; // for testing return RedirectToAction("Index"); } } public class ShoppingBasketItem { public int