Hello, I am just starting to play with ASP.NET MVC, I need to build a project for my school activities and I'm stuck with this (probably) simple thing, but I haven't got a clue...
I need to present a View that contains checkboxes to choose from, this part of the application allows an applicant for a job to enter his personal info, languages he speaks, education and work experience and skills. It's for IT crowd only.
So I handled the basic info, education and work experience using basic labels and textboxes, and now I wanna use checkboxes for choosing multiple languages that are spoken, and multiple skills.
Now, I am trying first to handle it by using DropDownLists, since I found an example like that in a tutorial, and then just modify it to use checkboxes.
Perhaps I should explain a little about the database design - the languages are stored in a "Language" table as simple as an ID and name, then the applicants are in the "Applicant" table and their relationship is stored in a M:N table "ApplicantSpeaksLanguage", containing the applicant's ID, the language's ID and a particular Weight's ID (another table).
Now, The appID is handled and not presented to be entered by the user, the weight ID - I';m just ignoring it for now, I am manually entering one valid value, and I wanna concentrate on the LanguageID column value - I will post the code here now. Now here's a part of the problem - I am presenting a DropDownList of language names to choose from, and when I post to the server I get a new record in the Language table, e.g. if English was entered and offered to the user at the start, and English is stored by the ID of 2, when a user selects English and posts, English is stored in the Languages table by another new ID, e.g. of 7, and then 7 is stored in the M:N table as the langID.. What I want is one of the existing langIDs to be entered in the M:N table, according to what language the user chose.
Maybe the problem is in the View...
I am using EntityFramework ORM and all of the tables are presented as classes - ApplicantSpeaksLang , Language etc...
I reckon this must be a beginner's mistake, but I just don't see it now.. And I haven't got a clue about working with checkboxes either...
Thanks for any help, regards
//GET: AddLanguages
[Authorize]
public ActionResult AddLanguages()
{
ApplicantSpeaksLang appLang = new ApplicantSpeaksLang();
Language language = new Language();
//Weight weight = new Weight();
var allLanguages = repository.getAllLanguages();
//var allWeights = repository.getAllWeights();
ViewData["Languages"] = new SelectList(allLanguages, language.name);
return View(appLang);
}
//POST: AddLanguages
[HttpPost,Authorize]
public ActionResult AddLanguages(FormCollection formValues)
{
ApplicantSpeaksLang appLang = new ApplicantSpeaksLang()
{
appID = repository.getApplicantByUsername(User.Identity.Name).applicantID
};
if (TryUpdateModel(appLang))
{
repository.AddAppSpeaksLang(appLang);
repository.Save();
return RedirectToAction("AddSkills");
}
Language language = new Language();
var allLanguages = repository.getAllLanguages();
ViewData["Languages"] = new SelectList(allLanguages, language.name);
return View(appLang);
}
Here's a part of the View:
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Language.name, ViewData["Languages"] as SelectList) %>
<%: Html.ValidationMessageFor(model => model.langID) %>
</div>