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. In the first view, Details of AuctionHouseController, I have:
<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
And here is the Details method:
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:
<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. 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
. The problem is, your model is of type Items
, yet you are trying to use EditorFor
for 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.