List <string>属性映射不起作用

时间:2018-09-20 12:23:28

标签: c# razor

我有一个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);
    } 

提交数据时,出现以下错误: enter image description here

呈现的形式如下: enter image description here

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

0 个答案:

没有答案