0

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.

5
  • try after changing from parameter name "thesis" to something else Commented Aug 28, 2014 at 12:40
  • I dont think it's from name, because if I use Thesis thesis, i get all for thesis. Everything is pass ok from antion to controller but after that ... maybe I must return result on other way but I don't know how. This is what i pass in request thesis.ThesisTitle:test ' thesis.ThesisDescription:test checkboxList:1 checkboxList:2 ' but in action - everything is null Commented Aug 28, 2014 at 12:49
  • Your confusing the ModelBinder because the name of the parameter in the action method is the same as the name of a property in your model. Change the method to public ActionResult CreateThesis(ThesisWithKeywordsModel model) and it should bind correctly Commented Aug 28, 2014 at 12:51
  • @Stephen Muecke, that's what I suggested, but it seems Trifon Dardzhonov didnot try it Commented Aug 28, 2014 at 12:53
  • WOW it helps :) I am new to asp mvc and i spent all my day to fix this stupid problem :D Many many many many thanks :) Commented Aug 28, 2014 at 13:00

1 Answer 1

1

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/>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.