以下代码可以正常运行:
@using (Html.BeginForm("LOLOL", "PATIENT", null))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>PATIENT</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</fieldset>
<p>
<input type="submit" value="SUBMIT" />
</p>
}
在LOLOLController中:
[HttpPost]
public IActionResult LOLOL(Patient p) {
var client = new MongoClient("mongodb://localhost:27017");
var userId = _userManager.GetUserId(HttpContext.User);
string db_name = "test" + userId;
var database = client.GetDatabase(db_name);
var collection = database.GetCollection<BsonDocument>("patients");
var filter = Builders<BsonDocument>.Filter.Eq("Name", p.Name.ToString());
var document = collection.Find(filter).First();
// I'm cutting short the rest of the code, because when I do something
// similar later, "collection.Find(filter).First()" fires an exception, I'll
// explain..
return View(p);
}
我有相当于取消HTML中的fieldset元素的东西,基本上只是在&#34; Html.BeginForm&#34;中的一个按钮,但是数据显然没有正确绑定,我知道,因为如果我只有一个按钮,没有数据输入,我点击按钮,然后我收到一个错误,说无法从数据库中找到数据。 (编辑:我现在已经确认这确实是因为Patient对象没有像我预期的那样传递给控制器,看起来像是在调用html.beginform时创建了一个全新的Patient对象...我想也许旧的Patient对象正在传递,因此每次使用Html.BeginForm时都不必输入所有数据成员。
总而言之,我想填写一个文本框,单击一个按钮来加载一个新页面并显示该文本框的值,但是该值仍然基本上保持在会话状态,所以如果我调用另一个Html.BeginForm函数并进入第三个视图,第一个视图中的文本将显示在第三个视图中,即使我没有在第二个视图中键入其值。希望我能重复这个过程,基本上加载一个类的数据成员,每个数据成员有一个视图。
答案 0 :(得分:1)
确保将上一个视图中的数据从Controller传递到新视图。传递它时,请在新视图的上一个视图中为这些属性添加@HiddenFor
。这样,新视图将保留,然后将值传递给下一个POST。
@Html.HiddenFor(model => model.PropertyYouPassedAndWantToKeepAndPassAgain
编辑:这是根据要求为一个对象使用多个视图的逻辑。
型号:
public class Patient
{
string Name { get; set; }
string Address { get; set; }
string City { get; set; }
}
Page1 GET:
[HttpGet]
public ActionResult Page1()
{
Patient patient = new Patient();
return View("~/Views/Page1.cshtml", patient);
}
Page 1查看...仅询问姓名。
@model mysite.Models.Patient
@using (Html.BeginForm("LOLOL", "PATIENT", null))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>PATIENT</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</fieldset>
<p>
<input type="submit" value="SUBMIT" />
</p>
}
Page1 POST ...找到病人并将其传递给下一个视图...
[HttpPost]
public ActionResult Page1(Patient patient)
{
if (ModelState.IsValid)
{
return View("~/Views/Page2.cshtml", patient); // pass your patient to the second page view with the name
}
else
{
return View("~/Views/Page1.cshtml", patient);
}
}
Page2 GET ...从之前的Page1 POST中获取患者并将其发送到Page2 View。
[HttpGet]
public ActionResult Page2(Patient patient)
{
// Receive patient from Page1 post and pass it to new view... includes the name
return View("~/Views/Page2.cshtml", patient);
}
Page2 View获取对象...使用HiddenFor保留您刚刚从GET发送的名称。
@model mysite.Models.Patient
@using (Html.BeginForm("LOLOL", "PATIENT", null))
{
@Html.HiddenFor(model => model.Name) @* This will keep the name on your next post *@
@Html.ValidationSummary(true)
<fieldset>
<legend>PATIENT</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
</fieldset>
<p>
<input type="submit" value="SUBMIT" />
</p>
}
由于HiddenFor持有姓名,因此会在您的下一篇文章中传递。它存在,但隐藏在表单本身之外。
[HttpPost]
public ActionResult Page2(Patient patient)
{
// Because of the HiddenFor, the Name will be passed because it was kept in the view... but hidden from the form itself.
// It's basically storing it for you to pass again
if (ModelState.IsValid)
{
// Pass object with Name and Address to next controller
return View("~/Views/Page3.cshtml", patient);
}
else
{
return View("~/Views/Page2.cshtml", patient);
}
}
Page2 POST
[HttpPost]
public ActionResult Page2(Patient patient)
{
// Because of the HiddenFor, the Name will be passed because it was kept in the view... but hidden from the form itself.
// It's basically storing it for you to pass again
if (ModelState.IsValid)
{
// Pass object with Name and Address to next controller
return View("~/Views/Page3.cshtml", patient);
}
else
{
return View("~/Views/Page2.cshtml", patient);
}
}
Page3 GET
[HttpGet]
public ActionResult Page3(Patient patient)
{
// Pass patient again... to your next view
return View("~/Views/Page3.cshtml", patient);
}
Page3查看...
@using (Html.BeginForm("LOLOL", "PATIENT", null))
{
@Html.HiddenFor(model => model.Name) @* Keep name again for your next post *@
@Html.HiddenFor(model => model.Address) @* Now we are keeping the address as well *@
@Html.ValidationSummary(true)
<fieldset>
<legend>PATIENT</legend>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
</fieldset>
<p>
<input type="submit" value="SUBMIT" />
</p>
}
依此类推......直到你的模型完成并希望用它做一些事情。