model-binding

How to dynamically create/remove elements and allow model binding to kick in?

陌路散爱 提交于 2019-11-30 10:26:52
I've done this in the past, and i may have to do it again, but before i do, i want to throw it out there to see how people handle it. Razor view: <ul> @Html.EditorFor(model => model.Questions) </ul> Which could produce: <ul> <li><input type="text" id="Questions_0__Title" name="Questions[0].Title" value="hi"/></li> <li><input type="text" id="Questions_1__Title" name="Questions[1].Title" value="hi2"/></li> <ul> Pretty dead simple. Now, i need to allow the user to add, edit or remove any of those "Questions". For Edit, it's easy - no work req'd. But for add/delete, if i were to do it with jQuery,

ASP.NET MVC 3 Model-binding and form fields

丶灬走出姿态 提交于 2019-11-30 09:18:46
I have an entity called Domain.Models.BlogPost which contains the following properties: PostID Title Author PostedDate Body I also have an entity called Domain.Models.PostComment which contains the following properties: CommentID PostID Author Email Website Body BlogPost contains many PostComments . A one to many relationship. Now I have a view like this (separated comment form from blog post code via html comment): @model Domain.Models.BlogPost @using Domain.Models; @{ ViewBag.Title = "Post"; } <div class="postTitle">@Model.Title</div> <div class="subInfo"> Posted by @Model.Author on @Model

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

故事扮演 提交于 2019-11-30 08:45:21
问题 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

How to bind view model property with different name

戏子无情 提交于 2019-11-30 08:38:17
Is there a way to make a reflection for a view model property as an element with different name and id values on the html side. That is the main question of what I want to achieve. So the basic introduction for the question is like: 1- I have a view model (as an example) which created for a filter operation in view side. public class FilterViewModel { public string FilterParameter { get; set; } } 2- I have a controller action which is created for GETting form values(here it is filter) public ActionResult Index(FilterViewModel filter) { return View(); } 3- I have a view that a user can filter

Why does the ASP.Net MVC model binder bind an empty JSON array to null?

白昼怎懂夜的黑 提交于 2019-11-30 08:07:22
Here is my model class: public class MyModel { public Employees[] MyEmpls{get;set;} public int Id{get;set;} public OrgName{get;set;} } Passing the below JSON structure object with MyEmpls as empty array to MVC controller. ["Id":12, "MyEmpls":[], "OrgName":"Kekran Mcran"] Controller [HttpPost] public ActionResult SaveOrg(MyModel model) { //model.MyEmpls is null here } I am expecting mode.MyEmpls to be an empty c# array, not a null. Is a custom model binder necessary to achieve an empty array? I think that some of the other answers have missed the meaning of the question: why does the default

WebAPI Custom Model binding of complex abstract object

我是研究僧i 提交于 2019-11-30 07:17:38
问题 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,

MVC Model Binding to a collection where collection does not begin with a 0 index

做~自己de王妃 提交于 2019-11-30 06:54:49
I'm trying to perform remote validation on a property of an item within a collection. The validation works OK on the first item of the collection. The http request to the validation method looks like: /Validation/IsImeiAvailable?ImeiGadgets[0].ImeiNumber=123456789012345 However on the 2nd item where the url looks like below, the validation doesn't work /Validation/IsImeiAvailable?ImeiGadgets[1].ImeiNumber=123456789012345 Now I'm pretty sure the reason for this, is that binding wont work on a collection that doesn't begin with a zero index. My validation method has a signature as below: public

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

大憨熊 提交于 2019-11-30 05:28:50
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.Decrypt(cookie.Value); if (!string.IsNullOrWhiteSpace(decrypted.UserData)) return JsonSerializer

How to trim spaces of model in ASP.NET MVC Web API

眉间皱痕 提交于 2019-11-30 03:58:16
问题 What is the best way to trim all the properties of the model passed to the MVC web api (post method with complex object). One thing simply can be done is calling Trim function in the getter of all the properties. But, I really do not like that. I want the simple way something like the one mentioned for the MVC here ASP.NET MVC: Best way to trim strings after data entry. Should I create a custom model binder? 回答1: To trim all incoming string values in Web API, you can define a Newtonsoft.Json

How does one perform asp.net mvc 4 model binding for enums?

a 夏天 提交于 2019-11-30 03:51:39
问题 Has the asp.net mvc team implemented a default model binding for enums? One that is out of the box and there is no need of creating a custom model binder for enums. UPDATE: Let's say I have an action that will be receiving a view model and a JSON object will be posted to the action. jsObj{id:2, name:'mike', personType: 1} and the view model: class ViewModel { public int id {get;set;} public string name {get;set;} public PersonType personType{get;set;} } public enum PersonType : int { Good = 1