Reputation: 3
Hi i tried to find a fix to my problem but i cant. I have a class with one Thesis and many keywords
public class ThesisWithKeywordsModel
{
public Thesis thesis { get; set; }
public IQueryable<Keyword> keywords { get; set; }
public IEnumerable<int> checkboxList { get; set; }
}
Action
[HttpGet]
public ActionResult CreateThesis()
{
ThesisWithKeywordsModel thesis = new ThesisWithKeywordsModel();
thesis.thesis = new Thesis();
thesis.keywords = db.KeywordRepository.GetAllKeywords();
thesis.checkboxList = new List<int>();
return View(thesis);
}
where I use thesis.keywords to show list of checkbox buttons and I put thesis.checkboxList to save returned result from view who must be ICollection(int) id's from checked checkbox buttons.
Here is my View
@model SearchSystem.Models.ThesisWithKeywordsModel
@{
var listField = new List<SelectListItem>();
listField.Add(new SelectListItem() { Text = "Софтуер", Value = "Софтуер", Selected = true });
listField.Add(new SelectListItem() { Text = "Хардуер", Value = "Хардуер" });
listField.Add(new SelectListItem() { Text = "Мрежи", Value = "Мрежи" });
var listFree = new List<SelectListItem>();
listFree.Add(new SelectListItem() { Text = "Свободна", Value = "Свободна", Selected = true });
listFree.Add(new SelectListItem() { Text = "Заета", Value = "Заета" });
}
@using (Html.BeginForm("CreateThesis", "ProfessorProfile", FormMethod.Post))
{
<table class="table">
<tr>
<td>@Html.LabelFor(x => x.thesis.ThesisTitle)</td>
<td>@Html.EditorFor(x => x.thesis.ThesisTitle)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.thesis.ThesisDescription)</td>
<td>@Html.EditorFor(x => x.thesis.ThesisDescription)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.thesis.Field)</td>
<td>@Html.DropDownList("Field", listField)</td>
</tr>
<!--<tr>
<td>@Html.LabelFor(x => x.thesis.Free)</td>
<td>@Html.DropDownList("Free", listFree)</td>
</tr>-->
</table>
foreach(var keyword in Model.keywords)
{
<input type="checkbox" id="@keyword.KeywordId" name="checkboxList" value="@keyword.KeywordId">
<label for="@keyword.KeywordId">@keyword.Value</label><br/>
}
<input type="submit" value="Create"/>
}
and here is my other action
[HttpPost]
public ActionResult CreateThesis(ThesisWithKeywordsModel thesis)
{
if (ModelState.IsValid)
{
var loggedProfessorId = db.ProfessorRepository.GetProfessorIDByUserName(User.Identity.Name);
if (loggedProfessorId != 0)
{
var professor = db.ProfessorRepository.GetProfessorById(loggedProfessorId);
db.ProfessorRepository.AddThesis(professor, thesis.thesis);
return RedirectToAction("MyTheses");
}
return RedirectToAction("Login", "Account");
}
return View(thesis);
}
The problem is the object ThesisWithKeywordsModel thesis is null, thesis.thesis, thesis.checkboxList are both null. Funny thing is that when i see response in response body I have POST parameters and if I change ThesisWithKeywordsModel thesis with Thesis thesis i have all values that i post from form.
Upvotes: 0
Views: 6003
Reputation: 11442
Firstly I would not attempt to use IQueryable for your Keywords
property, better would be to use IList. Secondly it would probably be better to combine Keywords and CheckboxList into a single property, having your keyword class something like this:
public class Keyword
{
public int KeywordId {get;set;}
public bool Checked {get;set;}
}
So ThesisWithKeywordsModel
would look like this:
public class ThesisWithKeywordsModel
{
public Thesis thesis { get; set; }
public IList<Keyword> keywords { get; set; }
}
Then in your controller, you should be able to do:
thesis.keywords = db.KeywordRepository.GetAllKeywords()
.Select(kw => new Keyword
{
KeyeordId = kw.KeywordId,
Checked = false
}).ToList();
Next you need to change your foreach
loop in the cshtml into a for
loop when passing a list of values to/from the view.
for(var i = 0; i < Model.keywords.Count; i++)
{
<input type="checkbox" id="@Model.keywords[i].KeywordId" name="checkboxList" checked='@Model.keywords[i].Checked ? "checked" : ""'>
<label for="@Model.keywords[i].KeywordId">@Model.keywords[i].Value</label><br/>
}
Upvotes: 1