model-binding

Problems with Model binding and validation attributes with asp.net Web API

时光毁灭记忆、已成空白 提交于 2019-12-09 06:55:24
I am writing a Web API with ASP.NET Web API, and make use of the following View Model. I seem to be having a problem with the data binding when there are two validation attributes on a particular property (i.e. [Required] and [StringLength(10)]). When posting a JSON value from a client to a controller action of the form: // POST api/list public void Post([FromBody] TaskViewModel taskVM) I observe the following: If I remove one of the multiple attributes everything is bound OK; If I leave in the multiple attributes, the client recieves a 500 internal server error and the body of the Post method

asp.net - MVC model binding to nested collection

可紊 提交于 2019-12-09 01:02:15
问题 I have a class: public class ClientInformation{ public string UserName {get; set;} public ICollection<RegionDistrictCity> RegionDistrictCity { get; set; } public class RegionDistrictCity { public string Region { get; set; } public string District { get; set; } public string City { get; set; } } } How should be formated the name attribute of input elements for properties Region, Distirct, City in html in order to make model binder populate collection "ICollection RegionDistrictCity"? I tried

How to have WebApi querystring parameter binding keep UTC dates?

大憨熊 提交于 2019-12-08 21:53:47
问题 I am sending in my querystring a UTC date, e.g. &EndDate=2000-01-02T03%3a04%3a05.0060000Z And my controller parameter binds it in a complex object, e.g. public async Task<DocumentsRequest> GetEchoFromUriDocumentsAsync( [FromUri] DocumentsRequest request) { return request; } What I've managed to figure out is that my object after parameter binding, changes my UTC date to a local date + offset. Here is a snippet of my test case [TestMethod] public void Should_generate_querystring_and_parameter

ASP.NET Core is using non-default constructor if no public default constructor found

对着背影说爱祢 提交于 2019-12-08 19:29:25
I am writing ASP.NET Core 2.2.0 application hosted on Service Fabric. I have a class which represents a request and I have declared two constructors: public for my own usage and private for serializers: public class MyClass { private MyClass() // for serializer { } public MyClass(string myProperty) // for myself { MyProperty = myProperty ?? throw new ArgumentNullException(nameof(myProperty)); } [Required] public string MyProperty { get; private set; } } Then, I created an API controller: [ApiController] public class MyController { [HttpPut] public async Task<IActionResult> Save([FromBody]

ModelState.IsValid is false when I have a nullable parameter

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:29:25
问题 I reproduced the issue I am having in a brand new MVC Web API project. This is the default code with a slight modification. public string Get(int? id, int? something = null) { var isValid = ModelState.IsValid; return "value"; } If you go to http://localhost/api/values/5?something=123 then this works fine, and isValid is true . If you go to http://localhost/api/values/5?something= then isValid is false . The issue I am having is that if you provide a null or omitted value for an item that is

ASP.Net Web Api not binding model on POST

拥有回忆 提交于 2019-12-08 15:01:16
问题 I'm trying to POST JSON data to a Web Api method but the JSON data is not binding to the model. Here's my model: [DataContract] public class RegisterDataModel { [DataMember(IsRequired = true)] public String SiteKey { get; set; } [DataMember(IsRequired = true)] public String UserId { get; set; } [DataMember(IsRequired = true)] public String UserName { get; set; } } Here's my Web Api action: public class RegisterController : ApiController { public Guid Post([ModelBinder] RegisterDataModel

How do I perform an Enum binding to a view's model in .NET's MVC?

 ̄綄美尐妖づ 提交于 2019-12-08 10:10:38
问题 How can I bind an enum into a drop down in MVC to make the model be valid after a post? Not sure does it need a converter or something else, I provide the code, what is your recommended solution? (the below code causes ModelError) Enum : public enum TimePlan { Routine = 0, Single = 1 } The Model : public TimePlan TheTimePlan { get; set; } public SelectListItem[] TimeList { get; set; } Controller : [HttpPost] public virtual ActionResult Education(EducationViewModel EducationModelInfo) { if

MVC DropDownList values posted to model aren't bound

此生再无相见时 提交于 2019-12-08 07:59:42
问题 DropDownLists are probably my least favourite part of working with the MVC framework. I have a couple of drop-downs in my form whose selected values I need to pass to an ActionResult that accepts a model as its parameter. The markup looks like this: <div class="editor-label"> @Html.LabelFor(model => model.FileType) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.FileType.Variety, (SelectList)ViewBag.FileTypes) </div> <div class="editor-label"> @Html.LabelFor(model =>

ASP.NET MVC razor view, post to a different model from the original model binding?

一曲冷凌霜 提交于 2019-12-08 07:26:55
问题 Well I have a group controller and view in the project, in which the model binding is GroupViewModel. But the group page is complex, and users will be able to make discussion topics. On this group view page, I have forms that allow users to post topics/replies. The model used for these forms can be TopicViewModel or ReplyViewModel, but the original model binding is only for GroupViewModel. It is declared at the beginning of the cshtml page: @model MyProject.ViewModels.GroupBrowseViewModel So

Model Bind to a List<> when Posting JavaScript Data Object

冷暖自知 提交于 2019-12-08 05:26:39
问题 I'm trying to post a JavaScript data object with the following: $.post(frm.attr("action"), data, function(res) { // do some stuff }, "json"); where 'data' takes the structure of data - panelId - siteId - ConfiguredFactsheetId // this is an array of CheckBox ids that correspond to ConfiguredFactsheets - 123 - 234 - 345 With this, both site & panel are correctly instantiated & bound with their data but the List object is null. public JsonResult Edit(Site site, Panel panel, List