model-binding

MVC4 bind model to ICollection or List in partial

橙三吉。 提交于 2019-11-29 11:09:18
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) { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Information" }}) </table> and my partial is

Deserialization / model binding in MVC4 webapi does not work with arrays

和自甴很熟 提交于 2019-11-29 11:03:18
I'm using the new WebApi which is part of MVC4 beta. I have the following class: public class Voucher { public string Id { get; set; } public string TableId { get; set; } public Product[] Products { get; set; } } My controller looks like this: public class VouchersController : ApiController { public Voucher PostVoucher(Voucher voucher) { //.... } } On the client side I serialize the data using an XmlSerializer . The output looks like expected and the Products array is serialized. If I post the data and put a break point inside the PostVoucher method, I get the data for Id and TableId , but

ASP.NET MVC - Problem with EditorTemplate for ICollection<T> mapped to Enum

荒凉一梦 提交于 2019-11-29 07:39:52
I have an ASP.NET MVC 3 (Razor) website, and a (simplified) model called Review : public class Review { public int ReviewId { get; set; } public bool RecommendationOne { // hook property - gets/set values in the ICollection } public bool RecommendationTwo { // etc } public ICollection<Recommendation> Recommendations { get; set; } } Recommendation is as follows: public class Recommendation { public byte RecommendationTypeId } I also have an enum called RecommendationType , which i use to map the above recommendation to. (based on RecommendationTypeId). So to summarize - a single Review has many

Finding custom attributes on view model properties when model binding

。_饼干妹妹 提交于 2019-11-29 05:46:51
I've found a lot of information on implementing a custom model binder for validation purposes but I haven't seen much about what I'm attempting to do. I want to be able to manipulate the values that the model binder is going to set based on attributes on the property in the view model. For instance: public class FooViewModel : ViewModel { [AddBar] public string Name { get; set; } } AddBar is just public class AddBarAttribute : System.Attribute { } I've not been able to find a clean way to find the attributes on a view model property in the custom model binder's BindModel method. This works but

How do I pass value to MVC3 master page ( _layout)?

不想你离开。 提交于 2019-11-29 04:02:39
问题 I have a custom modelbinder, its check the authentication cookie and return the value. public class UserDataModelBinder<T> : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext.RequestContext.HttpContext.Request.IsAuthenticated) { var cookie = controllerContext.RequestContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) return null; var decrypted = FormsAuthentication

Why does ModelState.IsValid fail for my ApiController method that has nullable parameters?

本秂侑毒 提交于 2019-11-29 03:23:27
I have an ApiController method that accepts several parameters, like so: // POST api/files public HttpResponseMessage UploadFile ( FileDto fileDto, int? existingFileId, bool linkFromExistingFile, Guid? previousTrackingId ) { if (!ModelState.IsValid) return Request.CreateResponse(HttpStatusCode.BadRequest); ... } When I POST to this I'm putting the FileDto object in the body of the request, and the other parameters on the query string. I've already discovered that I cannot simply omit the nullable parameters - I need to put them on the query string with an empty value. So, my query looks like

Successful Model Editing without a bunch of hidden fields

匆匆过客 提交于 2019-11-29 02:56:34
问题 In Short : How do I successfully edit a DB entry without needing to include every single field for the Model inside of the Edit View? UPDATE So I have an item in the DB (an Article). I want to edit an article. The article I edit has many properties (Id, CreatedBy, DateCreated, Title, Body). Some of these properties never need to change (like Id, CreatedBy, DateCreated). So in my Edit View, I only want input fields for fields that can be changed (like Title, Body). When I implement an Edit

WebAPI Custom Model binding of complex abstract object

扶醉桌前 提交于 2019-11-29 02:23:30
This is a tough one. I have an issue with binding a model from JSON. I am attempting to resolve polymorphic-ally the record supplied with the type of record that it will resolve to (I want to be able to add many record types in the future). I have attempted to use the following example to resolve my model when calling the endpoint however this example only works for MVC and not Web API applications. I have attempted to write it using IModelBinder and BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext). However I can't find the equivalent of ModelMetadataProviders in

ASP.NET MVC model binding foreign key relationship

心已入冬 提交于 2019-11-29 00:06:51
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) The form posts the values Name, Description and Manufacturer, where Manufacturer is a primary key of

ASP.NET MVC3 JSON Model-binding with nested class

百般思念 提交于 2019-11-28 22:16:41
问题 In MVC3, is it possible to automatically bind javascript objects to models if the model has nested objects? My model looks like this: public class Tweet { public Tweet() { Coordinates = new Geo(); } public string Id { get; set; } public string User { get; set; } public DateTime Created { get; set; } public string Text { get; set; } public Geo Coordinates { get; set; } } public class Geo { public Geo(){} public Geo(double? lat, double? lng) { this.Latitude = lat; this.Longitude = lng; } public