你可以像这样扩展你的视图模型。
视图模型
公共类 PeopleViewModel {
public List<string> EmailAddress { get; set; }
public string EmaiAddressString {
get {
string rValue = string.Empty;
EmailAddress.ForEach(x => rValue += (x + "\n" ));
return rValue;
}
set {
var newValue = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList<string>();
EmailAddress = newValue;
}
}
}
看法
@model SigKoExample.Models.PeopleViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@if (IsPost) {
<h2>New Values</h2>
@Html.TextArea("EmailAddresses", Model.EmaiAddressString.ToString())
} else {
using (Html.BeginForm()) {
@Html.TextAreaFor(m => m.EmaiAddressString)
@Html.HiddenFor(m => m.EmailAddress)
<input type="submit" value="Save" />
}
}
控制器
public ActionResult Index() {
PeopleViewModel model = new PeopleViewModel {
EmailAddress = new List<string> {
"ValueOne",
"ValueTwo"
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(PeopleViewModel model) {
return View(model);
}