模型绑定asp.net核心Web API

时间:2020-11-09 12:44:25

标签: 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.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(),我该如何解决呢?对不起,英语不好;)

1 个答案:

答案 0 :(得分:-1)

首先,您需要在视图中添加输入,以便可以在表单发布时传递ID。

这是一个演示:

Index.cshtml(我使用隐藏的输入,如果您要在视图中编辑ID,则可以删除hidden

 <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>

然后,在您的CustomModelBinder中,将其绑定到模型:

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;
        }

Controller(创建视图时,我将ID赋予视图)

public IActionResult Index()
        {
            return View(new MainCont {  Id=1});
        }

结果: enter image description here

相关问题