model-binding

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

孤人 提交于 2019-12-18 12:23:20
问题 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

Is there a reason why the default modelbinder doesn't bind to fields?

谁说我不能喝 提交于 2019-12-18 08:29:07
问题 I'm using ASP.NET MVC3 and i'm wondering that the default modelbinder binds to public properties but not to public fields. Normally i just define the model classes with properties but sometimes i use some predefined classes which contains some fields. And everytime i have to debug and remember that the modelbinder just don't like fields. The question: Whats the reason behind it? 回答1: but sometimes i use some predefined classes which contains some fields While I cannot answer your question

Possible to change Data Annotations during Runtime? (ASP.NET MVC's [Range] [Required] [StringLength] etc.)

匆匆过客 提交于 2019-12-18 06:26:32
问题 Normally, ModelBinding Validation of a class member might be done like this example: public Class someclass { [StringLength(50)] public string SomeValue { get; set; } } SomeValue is limited to 50 characters at a maximum. Is it possible to have the constant (50) changed to something else at run-time, say, during the construction of each instance of that class, so that it is possible to have varying instances with different StringLength limitations? If so, how does one do this? 回答1: Yes. But

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

淺唱寂寞╮ 提交于 2019-12-18 03:59:09
问题 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

Upgrading to MVC4 RC: No MediaTypeFormatter is available to read an object of type 'TestRequestModel' from content with media type ''undefined''

耗尽温柔 提交于 2019-12-17 20:57:42
问题 I've been using the MVC4 beta and am currently working to upgrade to the recently released RC version. It appears that model-binding complex request types has changed , but I can't figure out how / what I'm doing wrong. For example, say I have the following API controller: public class HomeApiController : ApiController { public TestModel Get() { return new TestModel { Id = int.MaxValue, Description = "TestDescription", Time = DateTime.Now }; } } This yields the expected result: <TestModel

asp.net core custom model binder just for one property

笑着哭i 提交于 2019-12-17 20:44:30
问题 I have a simple model for my asp.net core controller: [HttpPost] public async Task<DefaultResponse> AddCourse([FromBody]CourseDto dto) { var response = await _courseService.AddCourse(dto); return response; } My model is : public class CourseDto { public int Id { get; set; } public string Name { get; set; } public string Genre { get; set; } public string Duration { get; set; } public string Level { get; set; } public string AgeRange { get; set; } public string Notes { get; set; } public bool

Passing array of integers to webapi Method

为君一笑 提交于 2019-12-17 19:44:16
问题 I am trying to pass an array of int but I can not get the value in the webapi method var postData = { "deletedIds": deletedIds }; $.ajax({ type: "DELETE", traditional: true, dataType: 'json', contentType: 'application/json', cache: false, url: "/api/Management/Models", data: JSON.stringify(postData), success: ModelDeleted, error: ModelNotDeleted }); and in apiController : [HttpDelete] public bool DeleteModel(int[] deletedIds) { return modelsRepository.DeleteModels(deletedIds); } 回答1: Your

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

怎甘沉沦 提交于 2019-12-17 18:53:18
问题 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).

JavaScriptSerializer and ASP.Net MVC model binding yield different results

守給你的承諾、 提交于 2019-12-17 18:44:40
问题 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 {

Steve Sanderson's BeginCollectionItem helper won't bind correctly

99封情书 提交于 2019-12-17 12:40:08
问题 I am using Steve Sanderson's BeginCollectionItem helper and ran into a problem. I have a form that has an option to add unlimited reward fields. I am using his helper since it solved this problem with how to keep generating the fields and not have to worry about how to bind it when the form gets submitted. I have in this same form some checkboxes that there is an unknown amount. The difference with this one versus the rewards is the unknown amount will become known after a database call and