Reputation:
How can I set the default value for the DrowDownList?
I have something like this:
@Html.DropDownListFor(model => model.BusinessID, (SelectList)ViewBag.Businesses, new { @class = "form-control" })
EDIT:
// GET: Reviews/Create
public ActionResult Create()
{
ViewBag.Businesses = new SelectList(db.Businesses, "BusinessID", "BusinessName");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ReviewID,ReviewText,Rating,BusinessID")] Review review, int id)
{
if (ModelState.IsValid)
{
db.Reviews.Add(review);
db.SaveChanges();
return RedirectToAction("Details", "Businesses", new { id = review.BusinessID });
}
review.BusinessID = id;
ViewBag.Businesses = new SelectList(db.Businesses, "BusinessID", "BusinessName", review.BusinessID);
return View(review);
}
How I call it:
@Html.ActionLink("Write a review", "Create", "Reviews", new { id = Model.BusinessID }, null)
And the point is that each my BusinessID, has a value, and I call them let's say like this /Reviews/Create/15, so in this case BusinessID is 15. And when the page loads, I want the item with the BusinessID 15 to be selected by default. But whenever I load the page, it always selects the one with the lowest BusinessID.
UPDATE:
Changing GET action to this solved the problem:
public ActionResult Create(int? id)
{
ViewBag.Businesses = new SelectList(db.Businesses, "BusinessID", "BusinessName", id);
return View();
}
Upvotes: 1
Views: 14225
Reputation: 141
When you are passing the Model to the view, set BusinessID property of model. Use the following code :
//Assuming you bind the model to View using Create Action
public ActionResult Create()
{
ViewBag.Businesses = new SelectList(db.Businesses, "BusinessID", "BusinessName");
var reviewModel = new Review();
reviewModel.BusinessID = 15;
return View(reviewModel);
}
Main part is setting the value of "BusinessID" property while binding the model to view.
Upvotes: 2
Reputation: 93424
You really should be using the strongly typed versions, it makes this much easier, and less error prone.
@Html.DropDownListFor(x => x.BusinessID, Model.Businesses, new { @class="form-control" })
here. Model.Businesses is an IEnuerable (which can be a List, Collection, array, etc..)
Or you can use ViewBag, like so:
@Html.DropDownListFor(x => x.BusinessID, ViewBag.Businesses, new { @class="form-control" })
Now, i'm unclear on what you mean by "default value" here. Do you mean you want to set the value to a value that is in your database? In which case, you simply populate your model.BusinessID with the value you want it to be set to (make sure it's the value, not the text) and it will automatically select it when it renders the page.
If you mean you want something like "Select a Business..." then you would use the "option" overload:
@Html.DropDownListFor(x => x.BusinessID, ViewBag.Businesses, "Select a Business...", new {@class="form-control" })
Note: Do NOT name your list of SelectListItems (or your SelectList) the same as your Selected item property, ie.. do not do this:
@Html.DropDownListFor(x => x.BusinessID, ViewBag.BusinessID, "Select a Business...", new {@class="form-control" })
Upvotes: 3