[英]MVC 4 - Pass Data from View => Controller
The entity "Bond" has a property named "Kauf" that is another entity ("Price" = date, addedBy, value). 实体“ Bond”具有一个名为“ Kauf”的属性,该属性是另一个实体(“ Price” = date,add byBy,value)。
Now, when in Create View of Bond ( = Buy), a value for Price needs to be entered. 现在,在创建债券视图(=购买)时,需要输入价格值。 The standard Create View has no field for entering price data. 标准的“创建视图”没有用于输入价格数据的字段。
If I add 如果我加
<div class="editor-field">
@Html.EditorFor(model => model.Kauf.Value)
@Html.ValidationMessageFor(model => model.Kauf.Value)
</div>
to the view, then how would I be able to grasp that value in the controller, where regularly only the entity "Bond" is accepted as parameter? 从视图来看,那么我将如何能够在控制器中掌握该值,而通常只接受实体“债券”作为参数?
[HttpPost]
public ActionResult Create(Bond position)
Trying to access it through 尝试通过访问
position.Kauf.Value
would just reference the (yet) empty property "Kauf" from bond, I guess. 我猜只是会引用bond中的(尚未)空属性“ Kauf”。 Thank you for input! 感谢您的输入!
Posting as an answer as this is too long to comment. 发布作为答案,因为此评论太久了。 I have tried to recreate and it is all working here so I thought I would post what I have so you can compare with what you are doing: 我尝试重新创建,并且所有操作都在这里进行,所以我想我会发布我所拥有的内容,以便您可以与正在做的事情进行比较:
I am assuming that Kauf.Value
is a string here... 我假设Kauf.Value
在这里是一个字符串...
Controller 控制者
[HttpGet]
public ActionResult Create()
{
// Setup model before passing in
var model = new Bond();
return View(model);
}
[HttpPost]
public ActionResult Create(Bond position)
{
string theValue = position.Kauf.Value;
// At this point "theValue" contains a valid item
return View(position);
}
View 视图
@model MvcExperiments.Models.Bond
@using(@Html.BeginForm())
{
<div class="editor-field">
@Html.EditorFor(model => model.Kauf.Value)
@Html.ValidationMessageFor(model => model.Kauf.Value)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
If by "entity" you are referring to an actual ORM entity then that's probably what the issue is - you should be using view models for passing data to/from your views, not raw entities. 如果通过“实体”指的是实际的ORM实体,那么问题可能就出在这里-您应该使用视图模型来向/从视图传递数据,而不是原始实体。 For example, you can try the following: 例如,您可以尝试以下操作:
Model 模型
public class KaufViewModel
{
public double Price { get; set; }
public string Value { get; set; }
...
}
public class BondViewModel
{
public KaufViewModel Kauf { get; set; }
}
Controller 控制者
[HttpGet]
public ActionResult Create()
{
return View(new BondViewModel());
}
[HttpPost]
public ActionResult Create(BondViewModel bond)
{
// bond.Kauf.Value should be set at this point (given it's set in the form)
return View(bond); // fields should be re-populated
}
View 视图
@model BondViewModel
@using(@Html.BeginForm())
{
@Html.EditorFor(model => model.Kauf)
<p><input type="submit" value="Save" /></p>
}
None of the given answers here worked for me and I searched so much for a simple thing of passing data between action methods. 这里给出的答案都没有对我有用,因此我搜索了很多关于在操作方法之间传递数据的简单事情。 So, here is my answer. 所以,这是我的答案。
I have an action POST method that's called by a JS on a NavBar item click. 我有一个NavBar项目单击上的JS调用的操作POST方法。 This method instantiates a report instance, and passes it to be used by another action method. 此方法实例化一个报表实例,然后将其传递以供其他操作方法使用。 But here I'm returning a string clickedReport. 但是这里我返回一个字符串clickedReport。
The ViewBag didn't work for me, returning the instance as a Model didn't work, and the only way it worked was by either using TempData or Session as shown below. ViewBag对我不起作用,将实例作为模型返回不起作用,并且它起作用的唯一方法是使用TempData或Session,如下所示。
[HttpPost]
public ActionResult PopulateReport(int groupId, int itemId)
{
string clickedReport;
...
TempData["clickedReport"] = clickedReport;
Session["clickedReport"] = clickedReport;
return PartialView();
}
public ActionResult GridViewPartial()
{
// Now this other action method can get the clicked report assigned by the above action.
var tmpData = TempData["clickedReport"];
var sessionData = Session["clickedReport"];
// Runs the report instance and returns an object that holds a DataTable and other info to the grid that's in the calling partial view.
// Instantiate and populate GridConfig
GridConfig gridConfig = new GridConfig();
...
return PartialView("_GridViewPartial", gridConfig);
}
---- This is the view for the PopulateReport action ie PopulateReport.cshtml @Html.Action("GridViewPartial") ----这是PopulateReport操作的视图,即PopulateReport.cshtml @ Html.Action(“ GridViewPartial”)
Hope this helps someone. 希望这对某人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.