0

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 &raquo;</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?

3
  • 1
    Remove the @Value = " " code. If the value of Item.ItemId is 123, then it will be added automatically. You need to show your Details(int ID) method Commented Oct 28, 2015 at 22:43
  • @StephenMuecke public ActionResult Details(int id) { var item = _auctionhDbc.Items.Find(id); return View(item); } Commented Oct 28, 2015 at 23:00
  • Edit your question (not in comments). But the code in you comment does not appear to set the value of property Item.ItemId (and what is your model in the view? Based on what you have show you need item.Item.ItemId = id; which does not make much sense Commented Oct 28, 2015 at 23:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great! What if I need to access other properties of the item, e.g. Item.ItemName?
whatever your model carries with it, you can access the same way. i.e. @Html.EditorFor(m => m.YourModelProperty)
Consider also using a view model (a custom class for this) instead of your DbContext class Items. That way you will have more flexibility.
It helped me figure out the solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.