Asp.net mvc 模型绑定asp.net核心web api
我正在尝试绑定模型,但当我绑定智能手机或笔记本电脑道具时,我会丢失myContnet主道具id,以下是mainCont:Asp.net mvc 模型绑定asp.net核心web api,asp.net-mvc,asp.net-core,asp.net-web-api,Asp.net Mvc,Asp.net Core,Asp.net Web Api,我正在尝试绑定模型,但当我绑定智能手机或笔记本电脑道具时,我会丢失myContnet主道具id,以下是mainCont: `namespace Bind.Models { public class MainCont { public int Id{ get; set; } public Device Device { get; set; } } }` using Bind.Models; using Microsoft.AspNetCor
`namespace Bind.Models
{
public class MainCont
{
public int Id{ get; set; }
public Device Device { get; set; }
}
}`
using Bind.Models;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Bind
{
public class DeviceModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var model1 = new MainCont();
var select = bindingContext.ValueProvider.GetValue("select").FirstValue;
if (select == "SmartPhone")
{
var model2 = new SmartPhone();
model2.screensize = bindingContext.ValueProvider.GetValue("screensize").FirstValue;
model2.imei = bindingContext.ValueProvider.GetValue("imei").FirstValue;
model1.Device = model2;
}
else if (select == "Laptop")
{
var model2 = new Laptop();
model2.CPU = bindingContext.ValueProvider.GetValue("CPU").FirstValue;
model2.GPu = bindingContext.ValueProvider.GetValue("GPu").FirstValue;
model1.Device = model2;
}
bindingContext.Result = ModelBindingResult.Success(model1);
return Task.CompletedTask;
}
}
}
我无法设置id,当然是在DeviceBinder中,我编写了新的mainCont(),如何解决它?对不起,英语不好;) 首先,您需要在视图中添加一个输入,以便在表单发布时传递Id 下面是一个演示: cshtml(我使用隐藏输入,如果您想在视图中编辑Id,可以删除
隐藏的):
控制器(创建视图时,我为视图提供Id):
结果:
您不需要编写自己的IModelBinder
——只需定义一个DTO类型。不需要这样做。使用DTO,automapper将DTO绑定到数据库模型扫描你能帮我吗?我不知道我是怎么做的当然,我也这么做了,但是如果主要内容有很多道具呢?若我并没有绑定就获得了数据,那个么我会得到除设备之外的所有道具,若我使用绑定器,那个么我只会得到设备,并失去其他道具。是否有任何方法可以使用binder自动绑定设备和其他道具?这里是的文档,它只能逐个使用GetValue(string)获取值,因此无法在自定义模型绑定中自动绑定。
<form asp-action="create" asp-controller="home" method="post">
<input asp-for="Id" hidden>
<select id="select" name="select">
<option value="SmartPhone">SmartPhone </option>
<option value="Laptop">Laptop </option>
</select>
<div id="sample"></div>
<button type="submit">გაგზავნა</button>
</form>
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var model1 = new MainCont();
//bind the Id here
model1.Id= Convert.ToInt32(bindingContext.ValueProvider.GetValue("Id").FirstValue);
var select = bindingContext.ValueProvider.GetValue("select").FirstValue;
if (select == "SmartPhone")
{
var model2 = new SmartPhone();
model2.screensize = bindingContext.ValueProvider.GetValue("screensize").FirstValue;
model2.imei = bindingContext.ValueProvider.GetValue("imei").FirstValue;
model1.Device = model2;
}
else if (select == "Laptop")
{
var model2 = new Laptop();
model2.CPU = bindingContext.ValueProvider.GetValue("CPU").FirstValue;
model2.GPu = bindingContext.ValueProvider.GetValue("GPu").FirstValue;
model1.Device = model2;
}
bindingContext.Result = ModelBindingResult.Success(model1);
return Task.CompletedTask;
}
public IActionResult Index()
{
return View(new MainCont { Id=1});
}