model-binding

Is it possible to have a non-public parameterless constructor that can be used for model binding?

天涯浪子 提交于 2020-01-11 12:27:50
问题 I have a following POCO class. I don not want the parameterless constructor to be public. public class FileDownloadRequest { //public FileDownloadRequest() { } public FileDownloadRequest(int fileId, RepositoryFolderTypes fileType) //RepositoryFolderTypes is an enum, not a class { this.FileId = fileId; this.FileType = fileType; } public int FileId { get; set; } public RepositoryFolderTypes FileType { get; set; } //an enum } When I am trying a https://10.27.8.6/Files/DownloadFile?fileId=1

Select Items from List<T> in MVC 4 using Model Binding

丶灬走出姿态 提交于 2020-01-11 05:37:08
问题 Given a class public class Person { // Some general properties public List<Hobby> Hobbies { get; set; } } public class Hobby { // Some properties e.g. Name, etc. } static List<Hobby> AllHobbies { get; } Is it possible to create a view that allows the user to select his hobbies using model binding? It would certainly be possible in the view to loop through AllHobbies and render an <input type="checkbox" /> for each, then wire up the selected values by hand in the postback controller. It seems

[MVC3]Can't bind JSON array

☆樱花仙子☆ 提交于 2020-01-07 08:32:35
问题 I thought MVC3 can bind JSON data to model by default. but this code server: [HttpPost] public ActionResult Save(IList<int> IDs) { return null; } client: $.post('@Url.Action("Save", "Users")', {'IDs' : [1, 2, 3]}, function() {}); don't work. Why ?? 回答1: Your code sends IDs[]=1&IDs[]=2&IDs[]=3 . You need send IDs=1&IDs=2&IDs=3 . Set traditional:true parameter to use the traditional style of param serialization. $.ajax({ url: '@Url.Action("Save", "Users")', type: 'post', data: {'IDs' : [1, 2, 3

[MVC3]Can't bind JSON array

痞子三分冷 提交于 2020-01-07 08:32:09
问题 I thought MVC3 can bind JSON data to model by default. but this code server: [HttpPost] public ActionResult Save(IList<int> IDs) { return null; } client: $.post('@Url.Action("Save", "Users")', {'IDs' : [1, 2, 3]}, function() {}); don't work. Why ?? 回答1: Your code sends IDs[]=1&IDs[]=2&IDs[]=3 . You need send IDs=1&IDs=2&IDs=3 . Set traditional:true parameter to use the traditional style of param serialization. $.ajax({ url: '@Url.Action("Save", "Users")', type: 'post', data: {'IDs' : [1, 2, 3

asp.net MVC model binding with reflection

隐身守侯 提交于 2020-01-07 01:22:34
问题 You see my dilemma here. It's with the binding x => property.Name. So normally you would do something like x=> x.someproperty. But I'm using reflection so I can't do it that way. I tried a number of things. Using the viewdata to find the property. Doesn't like that. Any thoughts on the matter? foreach (var property in Model.GetType().GetProperties()) { <div class="row"> <div class="col-md-2"> <label>@(property.Name ?? property.Name)</label> </div> <div class="col-md-2"> @Html.TextBoxFor(x =>

mvc validating binded to entitiy with reference to other entities

主宰稳场 提交于 2020-01-06 19:25:49
问题 Hi just thought about mapping and binding my entity in controller. How should i correctly bind entity in model so i can use modelstate I am creating new MenuItem using MenuItemModel. public class MenuItemModel { public List<SelectListItem> Menus { get; set; } public MenuItem MenuItem { get; set; } } where my MenuItem class is defined as follows: public class MenuItem:Entity { public virtual int MenuItemId { get; set; } public virtual Menu Menu { get; set; } [Required] public virtual string

DefaultModelBinder behaviour when property absent from request

半城伤御伤魂 提交于 2020-01-06 05:35:08
问题 I have a model like the following: public class TestViewModel { string UpdateProperty { get; set; } string IgnoreProperty { get; set; } ComplexType ComplexProperty { get; set; } } where public class ComplexType { long? Code { get; set; } string Name { get; set; } } My controller action: public Edit(int id, FormColleciton formCollection) { var model = service.GetModel(id); TryUpdateModel(model); //... } When calling the Edit action I have a formCollection parameter containing only a key/value

MVC Model binding issue - list of objects

二次信任 提交于 2020-01-06 01:17:31
问题 The scenario is a shopping basket - the user will update a text box for the quantity and hit the update button. Currently, upon hitting the update button, the Action is hit but the model properties are all null. My first thoughts are that there is some issue with the binding because the ids of the controls are changed for each row. Are my thoughts correct? if so, how can I resolve this? EDIT: I also tried to use a for loop, rather than foreach but that didn't work either. All my code is below

Html.DropDownListFor does not bind boolean SelectList

泪湿孤枕 提交于 2020-01-05 08:09:32
问题 I have this code the constructs a select list as a boolean response var responseList = new List<SelectListItem>(); responseList.Add(new SelectListItem { Text = "Going", Value = bool.TrueString}); responseList.Add(new SelectListItem { Text = "Not Going", Value = bool.FalseString }); ViewData[ViewDataKeys.ResponseTo] = vatOptionList; In my view I use the dropdownlist helper below. @Html.DropDownListFor(m => m.ResponseTo, (IEnumerable<SelectListItem>)ViewData[ViewDataKeys.ResponseTo], "--Select-

Model binding issues with Kendo objects with complex child properties

試著忘記壹切 提交于 2020-01-05 03:59:08
问题 I have a Kendo UI grid which allows me to post multiple changes to the server. The model that is bound to the grid contains a list of a complex type. Here it is (simplified): public class User { public int ID { get; set; } public string Name { get; set; } public List<Role> Roles { get; set; } } To update the changes on the server I have a method with the following signature in my controller: public ActionResult UpdateUtilisateurs([DataSourceRequest] DataSourceRequest request, [Bind(Prefix =