c# - 列表属性映射不起作用
问题描述
我有一个 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 的新手。谢谢你的时间。
解决方案
推荐阅读
- python - 使用 Python 在新线程中创建 csv 文件
- python - ExtractMsg.py 检查附件并返回名称
- python - 无法运行 Python Kafka 生产者和消费者
- php - Laravel 通知未发送
- kubernetes - *4 connect() 在上游连接到 kubernetes 仪表板时失败(113:没有到主机的路由)
- html - 如何使用 flexbox 制作一堆项目
- c# - Azure Functions - SQL DB Extract to Output Blob
- amazon-web-services - 为什么我的 Swagger UI 在 https 上加载时会尝试访问非 http 内容?
- javascript - 以递归方式将firebase快照转换为数组无法正常工作
- php - 该函数不连接查询到数据库