Home How to select SelectListItem inside a Razor View

# How to select SelectListItem inside a Razor View

user13659
1#
user13659 Published in September 20, 2018, 4:35 pm

I have a Model which i want to edit (Location).This model has a field called ActivityId.I am sending an array of ActivityId-s via ViewData to the view and transform them to a SelectList. I want the ActivityId field (long) of the Location to be set with the selected item from the dropdownlist (string) - i need a conversion done somehow from string to long before entering the Post Action of the controller.(EditConfirmed)

Models:

 [Table("location")]
public partial class Location
{
public Location()
{
StoryLocation = new HashSet<StoryLocation>();
UserStoryLocation = new HashSet<UserStoryLocation>();
}

[Column("id", TypeName = "bigint(20)")]
public long Id { get; set; }
[Column("description")]
[StringLength(255)]
public string Description { get; set; }
[Column("name")]
[StringLength(255)]
public string Name { get; set; }

[Column("activity_id", TypeName = "bigint(20)")]
public long? ActivityId { get; set; }
[ForeignKey("ActivityId")]
[InverseProperty("Location")]
public Activity Activity { get; set; }
}
}

[Table("activity")]
public partial class Activity
{
public Activity()
{
Location = new HashSet<Location>();
}

[Column("activity_id", TypeName = "bigint(20)")]
public long ActivityId { get; set; }
[Column("description")]
[StringLength(255)]
public string Description { get; set; }
[Column("name")]
[StringLength(255)]
public string Name { get; set; }
[Column("type")]
[StringLength(255)]
public string Type { get; set; }

[InverseProperty("Activity")]
public ICollection<Location> Location { get; set; }
}


Controller:

    [HttpGet]
public IActionResult Edit(long id = 0)
{

Location loc = this.context.Locations.Find(id);

if (loc == null)
{
return NotFound();
}
ViewData[Constants.ViewData.TActivities]=this.context.Activities
.Select(elem=>
new SelectListItem
{
Text=elem.Name,
Value=elem.ActivityId.ToString()
}
).ToList();
return View(loc);
}


View

@using AdminMVC.Models
@using Newtonsoft.Json

@{
List<SelectListItem> dropActivities=ViewData[Constants.ViewData.TActivities] as List<SelectListItem>;
}

<html>
<body>
<div id="form">

</div>
<div id="page">
@using (Html.BeginForm("Edit","Location",FormMethod.Post))
{
<div id="table">
<label>Set Location:</label>
<table border="">

@Html.DisplayFor(x=>x.Id)
<tr>
<td>@Html.DisplayNameFor(x=>x.Name)</td>
<td>@Html.EditorFor(x=>x.Name)</td>
</tr>
<tr>
<td>@Html.DisplayNameFor(x=>x.Description)</td>
<td>@Html.EditorFor(x=>x.Description)</td>
</tr>

<div>
<label >Select Activity:</label>
@Html.DropDownList("Activity",dropActivities) //i need to somehow convert the selected value from the dropdown to long before the form is sent to the controller
</div>
</table>
</div>
<div id="control-panel">
<input type="submit" value="Edit">
</div>
}
</body>
</div>
</html>


Post to Controller

[HttpPost, ActionName("Edit")]
public IActionResult EditConfirmed(Location editLoc)

{
if (ModelState.IsValid)
{
this.context.Entry(editLoc).State = EntityState.Modified;
this.context.SaveChanges();
return RedirectToAction("Index");
}
return View(editLoc);
}


P.S:So far the ActivityId of the Location sent to the Post Action is null.I need it to be long.

• Show your model! Your binding to a property named Activity (not ActivityId) and if Activity is long it will be correctly bound – Stephen Muecke Feb 11 at 20:49
• I added it now. – Bercovici Adrian Feb 11 at 21:27
• Then its @Html.DropDownList("ActivityId", dropActivities) - but I suggest your read this Q/A to understand how to do this correctly – Stephen Muecke Feb 11 at 21:29
• Thank you very much setting the first parameter to ActivityId did the trick.I thought that the SelectList only takes the Text and Value parameters and that's it. – Bercovici Adrian Feb 11 at 21:34
• It does - but you binding the selected value to Activity which is a complex object - a <select> only posts back the value of selected option (in your case a long) – Stephen Muecke Feb 11 at 21:40