将数据从视图传递到.NET MVC中的控制器-"@model"不突出显示 [英] Passing data from a View to a Controller in .NET MVC - "@model" not highlighting
问题描述
以下代码可按需要运行:
The following code works as I need it to:
@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中:
In 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元素的方法,基本上只在"Html.BeginForm"中保留了一个按钮,但是数据显然没有正确绑定,这是因为我知道如果我只有一个按钮而且没有数据输入,我单击按钮,然后收到一条错误消息,提示无法从数据库中找到数据.(我现在已经确认这确实是因为Patient对象没有像我期望的那样传递给控制器,似乎是在调用html.beginform时创建了一个全新的Patient对象.旧的Patient对象正在传递,因此每次我们使用Html.BeginForm时我都不必输入其所有数据成员.
I have something equivalent to taking off the fieldset element in the HTML, leaving basically just a button in the "Html.BeginForm", but then the data is clearly not binding properly, which I know because if I just have a button and no data-entry, I click the button and then I get an error saying the data cannot be found from the database. ( I now have confirmed that this is indeed because the Patient object is not being passed to the controller quite as I expected it to, seems like a brand new Patient object was created upon calling html.beginform ... I thought that maybe the old Patient object was being passed so I did not have to enter all its data members every time we use Html.BeginForm)
总而言之,我想填写一个文本框,单击一个按钮以加载新页面并显示该文本框的值,但该值也基本上保持在会话状态,因此,如果我调用另一个Html.BeginForm函数并进入第三个视图,即使我不必在第二个视图中键入其值,第一个视图中的文本也将显示在第三个视图中.希望我可以重复一遍这个过程,并且实质上是通过每个数据成员一个视图加载类的数据成员.
In sum, I want to fill out a text box, click a button to load a new page and display the value of that textbox, but have that value also persisted in essentially a session state, so that if I call another Html.BeginForm function and go into a third view, the text from the first view will be displayed in the third view, even though I did not have to type its value in the second view. Hopefully I can repeat this process, and essentially load up the data members of a class with one view per data member.
推荐答案
确保从Controller将数据从上一个视图传递到新视图.传递它时,在新视图中为上一个视图的那些属性添加 @HiddenFor
.这样,新视图将保留,然后将值传递到下一个POST.
Make sure you pass the data from the previous view to the new view from your Controller. When you pass it, include @HiddenFor
for those properties from the previous view in your new view. That way the new view will keep and then pass the values to your next POST.
@ Html.HiddenFor(model => model.PropertyYouPassedAndWantToKeepAndPassAgain
这是对一个对象使用多个视图的逻辑……根据要求.
Here's the logic for using multiple views for one object... as requested.
型号:
public class Patient
{
string Name { get; set; }
string Address { get; set; }
string City { get; set; }
}
Page1 GET:
Page1 GET:
[HttpGet]
public ActionResult Page1()
{
Patient patient = new Patient();
return View("~/Views/Page1.cshtml", patient);
}
第1页视图...仅询问名称.
Page 1 View... only ask for the name.
@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 ...让患者接受治疗并将其传递到下一个视图...
Page1 POST... get the patient and pass it on to the next view...
[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.
Page2 GET... get the patient from the prior Page1 POST and send it off to the 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发送的名称.
Page2 View gets the object... use a HiddenFor to keep the name which you just sent from the 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拥有名称,它将在您的下一篇文章中传递.它在那里但对表单本身隐藏.
Since the HiddenFor holds the Name, it will be passed on your next post. It is there but hidden from the form itself.
[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
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
Page3 GET
[HttpGet]
public ActionResult Page3(Patient patient)
{
// Pass patient again... to your next view
return View("~/Views/Page3.cshtml", patient);
}
Page3视图...
Page3 View...
@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>
}
依次类推...直到完成模型并想对其进行处理.
And so on and so forth... until you have your model complete and want to do something with it.
这篇关于将数据从视图传递到.NET MVC中的控制器-"@model"不突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!