首页 > 解决方案 > 列表属性映射不起作用

问题描述

我有一个 List 属性(Mac),我将其映射如下:

查看型号:

public class UserViewModel
  {
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public virtual IList<string> Mac
        {
            get
            {
                return _Mac;
            }
            set
            {
                _Mac = value;
            }
        }
        private IList<String> _Mac;
        public string MacSerialized
        {
            get
            {
                return JsonConvert.SerializeObject(_Mac);
            }
            set
            {
                _Mac = JsonConvert.DeserializeObject<IList<string>>(value);
            }
        }
  }

剃刀视图:

@model RouterManagement.Models.UserViewModel

@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h3>Register</h3> <br />

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()



    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group row">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
        <div class="col-sm-10 col-md-3">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group row">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
        <div class="col-sm-10 col-md-3">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group row">
        @Html.LabelFor(model => model.Mac, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
        <div class="col-sm-10 col-md-3">
            <span class="add-new-icon glyphicon glyphicon-plus-sign" id="add_mac"> </span>
            @*Html.EditorFor(model => model.Mac, new { htmlAttributes = new { @class = "form-control", @id = "mac_addr" } })*@
            @Html.EditorFor(model => model.MacSerialized, new { htmlAttributes = new { @class = "form-control", @id = "mac_addr" } })
            @Html.ValidationMessageFor(model => model.MacSerialized, "", new { @class = "text-danger" })
            <!-- <input name="Mac" class="form-control" id="mac_addr" /> -->

        </div>
            </div>




            <div class="form-group row new_mac_wrapper">
                <div class="col-md-offset-3">
                    <div class="new_mac_container">
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <div class="col-md-offset-2 col-sm-10 col-md-2">
                    <input type="submit" value="Register" class="btn btn-primary col-sm-offset-1" />
                </div>
            </div>

        }

和行动:

 [HttpPost]
    public ActionResult Register(UserViewModel model)
    {
      CancellationTokenSource cancelToken = new CancellationTokenSource();

      AccountRegistrationRequestMessage requerstMessage = new AccountRegistrationRequestMessage();
      requerstMessage.FullName = model.Name; 
      requerstMessage.EmailAddress = model.Email;
      requerstMessage.MacAddresses = model.Mac;

      requerstMessage.RegistrationType = AccountRegistrationEnum.IndividualAccountRegistration;

      Task<AccountRegistrationResponseMessage> response = _interactor.Handle(requerstMessage, cancelToken.Token);

      UserViewModel viewModel = _presenter.Handle(response.Result, model, ModelState);

      if (response.Result.ValidationResult.IsValid)
      {
        //return View("DetailView", viewModel);
      }



      return View(viewModel);
    } 

当我提交数据时,我收到以下错误: 在此处输入图像描述

渲染形式如下: 在此处输入图像描述

在这里,Mac 的额外字段是用 jquery 创建的。我的方法有什么问题?不要骂我,因为我是 ASP.Net 的新手。谢谢你的时间。

标签: c#razor

解决方案


推荐阅读