[英]ASP.NET MVC Passing URL id between views of different controllers
So I decided to make an auction house web application as my first asp.net mvc project and I cannot figure out how to pass a parameter between two views that belong to different controllers. 因此,我决定制作一个拍卖行Web应用程序作为我的第一个asp.net mvc项目,但我不知道如何在属于不同控制器的两个视图之间传递参数。 In the first view, Details of AuctionHouseController, I have: 在第一个视图中,AuctionHouseController的详细信息中,我有:
<a class="btn btn-default" href="@Url.Action("Create", "Auctions", new { id = Model.ItemId })">Start Auction »</a>
and a URL: http://localhost:2142/AuctionHouse/Details/123 和URL: http:// localhost:2142 / AuctionHouse / Details / 123
And here is the Details method: 这是Details方法:
public ActionResult Details(int id)
{
var item = _auctionhDbc.Items.Find(id);
return View(item);
}
I want to pass the id part of the URL - the "123" to the view where the button leads - Create of AuctionsController, where I have: 我想将URL的id部分-“ 123”传递到按钮所指向的视图-创建AuctionsController,在其中:
<div class="form-group">
@Html.LabelFor(model => model.Item.ItemId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item.ItemId, new { htmlAttributes = new { @class = "form-control", @Value = " " } })
@Html.ValidationMessageFor(model => model.Item.ItemId, "", new { @class = "text-danger" })
</div>
</div>
I want to place the "123" as the default value (@Value) of the Html Editor field. 我想将“ 123”作为“ HTML编辑器”字段的默认值(@Value)。 How can I do that? 我怎样才能做到这一点?
Assuming you are using strongly typed views, your model for the Create
view will already have the value of 123 in ItemID
. 假设您使用的是强类型视图,那么用于Create
视图的模型在ItemID
中将已经具有值123。 The problem is, your model is of type Items
, yet you are trying to use EditorFor
for model.Item.ItemID
. 问题是,你的模型是类型的Items
,但你要使用EditorFor
为model.Item.ItemID
。
Thus, instead of your line 因此,代替您的行
@Html.EditorFor(model => model.Item.ItemId,
new { htmlAttributes = new { @class = "form-control", @Value = " " } })
if you use 如果您使用
@Html.EditorFor(model => model.ItemId,
new { htmlAttributes = new { @class = "form-control" } })
you will already have passed the value there. 您将已经在那里传递了值。 Make sure you use strongly typed views by putting: 确保通过以下方式使用强类型视图 :
@model YourNameSpace.Items
in the beginning of your view. 在您的观点的开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.