我正在尝试使用一个视图,其中显示当前结果,该结果可以添加新记录。我查看了this帖子以及this帖子并拼凑了一些我认为应该有效的内容,但它不会保存到数据库中。这是我的观点模型:
public class LabIndexViewModel
{
public Lab Lab { get; set; }
public IEnumerable<Lab> Labs { get; set; }
}
在我的控制器中,我在索引中有这个:
public ActionResult Index(int patid = 0, Lab lab = null)
{
ViewBag.Finalize = PatientSubmitted(patid);
ViewBag.DispPatientId = patid;
ViewBag.CheckButtonStatus = ButtonSubmitted(patid);
var labs = db.Labs.Where(l => l.PatientId == patid && l.Active);
LabIndexViewModel model = new LabIndexViewModel();
model.Labs = labs.ToList();
model.Lab = lab;
SetViewBagLists();
return View(model);
}
然后在我的帖子中,它将无法保存:
[HttpPost]
public ActionResult Create(LabIndexViewModel labindex)
{
ViewBag.DispPatientId = labindex.Lab.PatientId;
Lab lab = labindex.Lab;
try
{
lab.Active = true;
db.Labs.Add(lab);
db.SaveChanges();
return RedirectToAction("Index", "Lab", new { patid = lab.PatientId });
}
catch
{
ViewBag.Phase = new SelectList(StatusList(), "Text", "Value");
ViewBag.Name = new SelectList(db.LabOptions, "Test", "Value", lab.Name);
return View(lab);
}
}
以下是我在视图中提交数据的部分内容:
@model PamperWeb.Models.LabIndexViewModel
@using (Html.BeginForm("Create", "Lab")) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Lab</legend>
<tr>
<td>
@Html.DropDownList("Name", String.Empty)
@Html.ValidationMessageFor(model => model.Lab.Name)
</td>
<td>
@Html.EditorFor(model => model.Lab.Value)
@Html.ValidationMessageFor(model => model.Lab.Value)
</td>
<td>
@Html.EditorFor(model => model.Lab.Given)
@Html.ValidationMessageFor(model => model.Lab.Given)
</td>
<td>
@Html.EditorFor(model => model.Lab.TimeGiven)
@Html.ValidationMessageFor(model => model.Lab.TimeGiven)
</td>
<td>
@Html.DropDownList("Phase", String.Empty)
@Html.ValidationMessageFor(model => model.Lab.Phase)
</td>
@Html.HiddenFor(model => model.Lab.PatientId)
<td>
<input type="submit" value="Create" />
</td>
</tr>
</fieldset>
}
有人知道如何完成这项工作或有一个很好的例子吗?
答案 0 :(得分:1)
我没有真正了解所有问题,但我在那里看到了一些错误:
1)你的PartialView必须发布一个实验室,所以让它强烈输入Lab,因为HTML Helpers将生成HTML,默认的ModelBinder无法使用LabIndexViewModel在服务器中构建模型:
@model PamperWeb.Models.Lab
@using (Html.BeginForm("Create", "Lab")) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Lab</legend>
<tr>
<td>
@Html.DropDownList("Name", String.Empty)
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.EditorFor(model => model.Value)
@Html.ValidationMessageFor(model => model.Value)
</td>
<td>
@Html.EditorFor(model => model.Given)
@Html.ValidationMessageFor(model => model.Given)
</td>
<td>
@Html.EditorFor(model => model.TimeGiven)
@Html.ValidationMessageFor(model => model.TimeGiven)
</td>
<td>
@Html.DropDownList("Phase", String.Empty)
@Html.ValidationMessageFor(model => model.Phase)
</td>
@Html.HiddenFor(model => model.PatientId)
<td>
<input type="submit" value="Create" />
</td>
</tr>
</fieldset>
}
2)更改控制器操作“创建”以作为参数接收已发布的实验室:
[HttpPost]
public ActionResult Create(Lab lab)
{
ViewBag.DispPatientId = Lab.PatientId;
try
{
lab.Active = true;
db.Labs.Add(lab);
db.SaveChanges();
return RedirectToAction("Index", "Lab", new { patid = lab.PatientId });
}
catch
{
ViewBag.Phase = new SelectList(StatusList(), "Text", "Value");
ViewBag.Name = new SelectList(db.LabOptions, "Test", "Value", lab.Name);
return View(lab);
}
}
3)使用创建的ViewModel来显示实验室!这是ViewModel主要用途,在视图中显示复杂类型!任何其他操作都需要创建自定义ModelBinder来整合请求并在服务器中重新构建模型。
希望这对你有所帮助!我真的从问题中得到了这个!
答案 1 :(得分:0)
在评论的帮助下,我能够找出问题所在。当我将参数添加到html.beginform时,它不再使用patientid发送我的url参数。不知道为什么?我的普通创建视图有这个,所以我的隐藏参数获取了值。我最终在控制器中设置了值,因此表单中的隐藏参数能够获取它。以下是我的get索引现在解决了这个问题:
public ActionResult Index(int patid = 0, Lab lab = null)
{
ViewBag.Finalize = PatientSubmitted(patid);
ViewBag.DispPatientId = patid;
ViewBag.CheckButtonStatus = ButtonSubmitted(patid);
var labs = db.Labs.Where(l => l.PatientId == patid && l.Active);
LabIndexViewModel model = new LabIndexViewModel();
if (lab == null)
lab = new Lab();
lab.PatientId = patid;
model.Labs = labs.ToList();
model.Lab = lab;
SetViewBagLists();
return View(model);
}
我还发现了这个post helpful,我发现我可以指定一个模型发送给部分模型。