I have used scaffolding templates to create the necessary parts in the Controller and the accompanying Views for my Model class Question pictured here:
public class Question
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int QuestionId { get; set; }
[StringLength(250, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "MaxCharsQuestionAchieved")]
public string GeneralQuestion { get; set; }
[StringLength(1000, ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "MaxCharsExplanationAchieved")]
public string Explanation { get; set; }
public bool IsTemplate { get; set; }
public QuestionState QuestionState { get; set; }
public DateTime DateSubmitted { get; set; }
public DateTime? JudgementDate { get; set; }
public virtual Regular Author { get; set; }
public virtual List<Politician> AddressedPoliticians { get; set; }
public virtual List<Topic> Topics { get; set; }
public virtual Moderator LastEditor { get; set; }
public virtual List<Attachment> Attachments { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual List<QuestionSubscription> QuestionSubscriptions { get; set; }
public int FbShares { get; set; }
public int FbLikes { get; set; }
public int TwitterShares { get; set; }
public int SiteVotes { get; set; }
}
I'd like to change the template for the Create View. As you can see Question has a list of AddressedPoliticians. I'd like to present the users with a dropdownlist of all the Politicians in my Database. The value has to be the id of the Politician, the display member should be their FirstName. The View has a model of type Question, of course. How can I construct the dropdownlist and give the selected value to my Create(Question question) ActionResult method, so I can get the Politician out of the database to add it to AddressedPOliticians in Question?
I tried fiddling with ViewBag, but can't seem to get it to work. Hope someone can give me some tips/ suggestions.
EDIT:
I should probably show what I already tried :)
ActionResult methods:
public ActionResult Create()
{
ViewBag.PolIds = new SelectList(context.Politicians.ToList(), "UserId", "FirstName");
return View();
}
//
// POST: /Question/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Question question)
{
if (ModelState.IsValid)
{
var polId = ((SelectList)ViewBag.PolIds).SelectedValue;
Politician pol = context.Politicians.Single(p => p.UserId == (int)polId);
question.AddressedPoliticians.Add(pol);
question.DateSubmitted = DateTime.Now;
manager.CreateQuestion(question);
return RedirectToAction("Index");
}
return View(question);
}
Create.cshtml:
@model PoliticiOnline.DTO.Question
@{
ViewBag.Title = "Stel een vraag!";
}
<h2>Stel een vraag!</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Vraag</legend>
<div class="general-question">
<div class="editor-label">
@Html.LabelFor(model => model.GeneralQuestion, "Algemene Vraag")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.GeneralQuestion)
@Html.ValidationMessageFor(model => model.GeneralQuestion)
</div>
</div>
<div class="geadresseerde-politici">
@Html.Label("Politicus")
@Html.DropDownList("PolIds", "Kies een politicus...")
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Explanation, "Extra Uitleg")
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Explanation)
@Html.ValidationMessageFor(model => model.Explanation)
</div>
<p>
<input type="submit" value="Indienen!" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}