model-binding

ASP.NET MVC 5 Model-Binding Edit View

廉价感情. 提交于 2019-12-05 07:52:39
I cannot come up with a solution to a problem that's best described verbally and with a little code. I am using VS 2013, MVC 5, and EF6 code-first; I am also using the MvcControllerWithContext scaffold, which generates a controller and views that support CRUD operations. Simply, I have a simple model that contains a CreatedDate value: public class WarrantyModel { [Key] public int Id { get; set; } public string Description { get; set; } DateTime CreatedDate { get; set; } DateTime LastModifiedDate { get; set; } } The included MVC scaffold uses the same model for its index, create, delete,

.NET core custom and default binding combined

柔情痞子 提交于 2019-12-05 05:59:37
I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof

How to override the ASP.NET MVC 3 default model binder to resolve dependencies (using ninject) during model creation?

核能气质少年 提交于 2019-12-05 05:34:04
I have an ASP.NET MVC 3 application that uses Ninject to resolve dependencies. All I've had to do so far is make the Global file inherit from NinjectHttpApplication and then override the CreateKernel method to map my dependency bindings. After that I am able to include interface dependencies in my MVC controller constructors and ninject is able to resolve them. All that is great. Now I would like to resolve dependencies in the model binder as well when it is creating an instance of my model, but I do not know how to do that. I have a view model: public class CustomViewModel { public

.NET Core Custom model binder call default model binder

折月煮酒 提交于 2019-12-05 05:22:23
I'm trying to implement some middleware in .NET Core that rounds decimals to 2 decimal places. All of the other mapping can work as it currently does via the ComplexTypeModelBinder . I've tried calling that binder before mine or inheriting from it, but it ends up with the model just being null when it hits the controller. Essentially I'm after the same functionality as asked here: Call Default Model Binder from a Custom Model Binder? , but for .NET core. user11955020 public enum OPERATION { none, add, del, edit, excel } [ModelBinder(BinderType = typeof(JqGridModelBinder))] public class

ASP.NET Core Posting Array Object JSON

江枫思渺然 提交于 2019-12-05 03:42:33
I'm trying to post an array of Object in my view to my controller but params are null i saw that for just a simple object I need to put [FromBody] in my controller action. Here is my JSON: { "monJour": [ { "openTime": "04:00", "closedTime": "21:30", "id": "0" }, { "openTime": "08:00", "closedTime": "17:30", "id": "1" }, { "openTime": "08:00", "closedTime": "17:30", "id": "2" }, { "openTime": "08:00", "closedTime": "17:30", "id": "3" }, { "openTime": "08:00", "closedTime": "17:30", "id": "4" }, { "openTime": "08:00", "closedTime": "17:30", "id": "5" }, { "openTime": "08:00", "closedTime": "17

.NET Core 2.2 Required Properties when object is not null. Complex Nested Model

北慕城南 提交于 2019-12-05 02:51:31
问题 In .NET Core I have a model that looks like this. public class Home { [Required] public int Number {get;set;} public Address Address {get;set;} // This is not required } public class Address { [Required] public string Street {get;set;} public IFormFile Picture {get;set;} } In a controller method that looks like this [HttpPost] public string AddHomes([FromForm]List<Home> homes) { if(!ModelState.IsValid) { return BadRequest(); } // Do some saving return Ok(); } In a form request that looks like

What's the WebApi [FromUri] equivalent in ASP.NET MVC?

笑着哭i 提交于 2019-12-04 23:59:28
In WebApi I can decorate a parameter on a controller action with [FromUri] to have the components of the URI 'deserialized', if you will, into a POCO model; aka model-binding. Despite using MVC since 2.0, I've never used it for websites (dunno why). What's the equivalent of it in ASP.NET MVC 5? The attribute doesn't seem to be recognised in the IDE unless I need to reference a library. I'd like ~/thing/2014/9 to bind to the model below: public class WhateverModel { public int Year { get; set; } public int Month { get; set; } } Thanks Update In the other question (link above), the OP says:

asp.net 4.5 webforms model binding : client side validation supported?

强颜欢笑 提交于 2019-12-04 23:56:50
问题 I'm a huge fan of asp.net 4.5 webforms model binding using data annotations. ascx: <asp:FormView ItemType="Contact" runat="server" DefaultMode="Edit" SelectMethod="GetContact" UpdateMethod="SaveContact"> <EditItemTemplate> <asp:ValidationSummary runat="server" ID="valSum" /> Firstname: <asp:TextBox runat="server" ID="txtFirstname" Text='<%#: BindItem.Firstname %>' /> Lastname: <asp:TextBox runat="server" ID="txtLastname" Text='<%#: BindItem.Lastname %>' /> Email: <asp:TextBox runat="server"

ASP.NET MVC5: Want to update several items in Collection with model binding

女生的网名这么多〃 提交于 2019-12-04 22:05:34
问题 So I have a Collection of User objects, which should be mass-editable (edit many users at the same time). I'm saving the user input to the database using Entity Framework. The collection the controller method gets from the form is null. Why? Also, is the BindAttribute possible to use with collections like in my code? View: @model IEnumerable<Domain.User> @using (Html.BeginForm("UpdateUsers", "Users")) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) foreach (var item in Model) { @Html

How can I do model binding and data display at the same time using the same model.list?

*爱你&永不变心* 提交于 2019-12-04 21:53:48
I have a code similar to this: My View @model MyCode.Models.RemarksModel foreach (var row in Model.List) { <tr> <td align="center"><font class="font_table_content">@row.Id</font></td> <td align="center"><font class="font_table_content">@row.Full_Name</font></td> <td align="center"><font class="font_table_content">@Html.TextBox("row.Remarks")</font></td> } My Model public class RemarksModel { public IList<UserRemarks> List { get; set; } } My UserRemarks Object public class UserRemarks { public virtual string Id { get; set; } public virtual string Full_Name { get; set; } public virtual string