model-binding

Dynamic list of checkboxes and model binding

白昼怎懂夜的黑 提交于 2019-11-28 21:32:07
I'm trying to create a view that contains a list of checkboxes that is dynamically created from a database, and then retrieve the list of selected ones when the form is posted back. My EF model contains a class: public class ItemIWouldLikeACheckboxFor { public int Id { get; set; } public string Description { get; set; } } I have a view model that contains a list of these: public class PageViewModel { // various other properties public List<ItemIWouldLikeACheckboxFor> checkboxList { get; set; } } My controller get method: public ActionResult Create() { var viewModel = new PageViewModel();

Model Bind List of Enum Flags

拜拜、爱过 提交于 2019-11-28 15:10:09
问题 I have a grid of Enum Flags in which each record is a row of checkboxes to determine that record's flag values. This is a list of notifications that the system offers and the user can pick (for each one) how they want them delivered: [Flag] public enum NotificationDeliveryType { InSystem = 1, Email = 2, Text = 4 } I found this article but he's getting back a single flag value and he's binding it in the controller like this (with a days of the week concept): [HttpPost] public ActionResult

Best way to do partial update to ASP.NET MVC model ('merge' user submitted form with model)

ⅰ亾dé卋堺 提交于 2019-11-28 13:47:19
问题 My question is basicially the same as the situation presented in this stack overflow question where I find myself wanting to load the existing valid version of model from the DB, and update a portion of it as a certain sub-set of fields are exposed on my web form. Is there anyway I can make the Model Binding process guarantee that my ID property will be bound first? If I could guarantee this one thing, then, inside the setter of my ViewModel's ID property, I could trigger a 'load', so that

asp.net core custom model binder just for one property

为君一笑 提交于 2019-11-28 13:33:05
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 Active { get; set; } public string OrganisationCode { get; set; } } I'm trying to set value of

How to configure a One-to-Many relationship in EF

二次信任 提交于 2019-11-28 13:24:51
I have the following model public class PageConfig : Base { // Properties Etc.. public ICollection<Image> ScrollerImages { get; set; } } My approach is to bind using a junction table { PageConfigID, ImageID }. In my model binder i tried the following.. modelBuilder.Entity<PageConfig>() .HasMany(x => x.ScrollerImages) .WithMany() .Map(x => { x.ToTable("junc_PageConfigScrollerImages"); x.MapLeftKey("PageConfigID"); x.MapRightKey("ImageID"); }); Which results in a null collection of images. How can i bind these Images to the PageConfig model? EDIT Most of the problem was due to user error. jic

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

…衆ロ難τιáo~ 提交于 2019-11-28 12:11:26
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 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/xxxx">

Passing array of integers to webapi Method

断了今生、忘了曾经 提交于 2019-11-28 11:25:13
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); } Shashank Your code looking pretty Ok to me. Please define structure of "deletedIds" object. one suggestion is to Use

Changing the parameter name Web Api model binding

China☆狼群 提交于 2019-11-28 09:52:20
I'm using Web API model binding to parse query parameters from a URL. For example, here is a model class: public class QueryParameters { [Required] public string Cap { get; set; } [Required] public string Id { get; set; } } This works fine when I call something like /api/values/5?cap=somecap&id=1 . Is there some way I can change the name of the property in the model class but keep the query parameter name the same - for example: public class QueryParameters { [Required] public string Capability { get; set; } [Required] public string Id { get; set; } } I thought adding [Display(Name="cap")] to

Model binding in the controller when form is posted - navigation properties are not loaded automatically

感情迁移 提交于 2019-11-28 09:44:16
问题 I'm using the Entity Framework version 4.2. There are two classes in my small test app: public class TestParent { public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual ICollection<TestChild> TestChildren { get; set; } } public class TestChild { public int TestChildID { get; set; } public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual TestParent TestParent {

ASP.NET MVC - drop down list selection - partial views and model binding

喜夏-厌秋 提交于 2019-11-28 09:29:22
I'm fairly new to ASP.NET MVC and am trying to work out the best way to do this. It's probably simple but I just want to do things correctly so I thought I'd ask. Lets say I have a model that is this: Task - Id, Description, AssignedStaffMember StaffMember - Id, FirstName, LastName and in my view I want to create a new task. I make a strongly typed Razor view, and can use EditorFor to create textboxes for Description but what about AssignedStaffMember? I want a drop down list of all current staff and have the option of selecting one, then this gets submitted to an action method which is