1

I am getting a NullReference when I try to access the posted model. What am I doing wrong? I've tried everything I can think of, but I must be missing something simple? Here is my relevant code:

Controller

    public ActionResult EditQuestion(int id)
    {
        IFeedbackRepository rep = DAL.RepositoryFactory.GetFeedbackRepository();
        var q = rep.GetQuestion(id);
        SurveyQuestionEditModel question = new SurveyQuestionEditModel()
        {
            Id=q.Id,
            IsFreeText=q.FreeTextResponse,
            SurveyId=q.SurveyId,
            Question=q.Question,
            Category=q.Category
        };
        return View(question);
    }

    [HttpPost]
    public ActionResult EditQuestion(SurveyQuestionEditModel question)
    {
        IFeedbackRepository rep = DAL.RepositoryFactory.GetFeedbackRepository();
        rep.UpdateSurveyQuestion(question.Id, question.Question, question.IsFreeText, question.Category);
        return RedirectToAction("Edit", new { id = question.SurveyId });
    }

Relevant portion of ASPX:

    <fieldset>            
            <%: Html.HiddenFor(model => model.Id) %>
            <%: Html.HiddenFor(model => model.SurveyId) %>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Question) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Question) %>

Which creates:

   <form action="/feedback/Survey/EditQuestion" method="post">

    <fieldset>
        <legend></legend>

            <input id="Id" name="Id" type="hidden" value="72" />
            <input id="SurveyId" name="SurveyId" type="hidden" value="4" />

        <div class="editor-label">
            <label for="Question">Question</label>
        </div>
        <div class="editor-field">
            <input id="Question" name="Question" type="text" value="Test" />

        </div>

        <div class="editor-label">
            <label for="Category">Question Category</label>
        </div>
        <div class="editor-field">
            <input id="Category" name="Category" type="text" value="Test" />

        </div>

        <div class="editor-label">
            <label for="IsFreeText">Does this question require a free text response?</label>
        </div>
        <div class="editor-field">
            <input checked="checked" id="IsFreeText" name="IsFreeText" type="checkbox" value="true" /><input name="IsFreeText" type="hidden" value="false" />

        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

</form>

Here is the model class:

    public class SurveyQuestionEditModel
    {


        public int Id { get; set; }

        [DisplayName("Question")]
        [Required]
        [MinLength(4)]
        public string Question { get; set; }

        [DisplayName("Does this question require a free text response?")]
        public bool IsFreeText { get; set; }

        public int SurveyId { get; set; }

        [DisplayName("Question Category")]
        [Required]
        public string Category { get; set; }
    }
7
  • I might be wrong, but I am pretty sure your Id fields should be e.g. "ModelType.PropertyName" and not just "PropertyName", unless that's just when your rendering multiple types in one model. Commented Dec 20, 2012 at 13:04
  • Why would editorFor not output those? Commented Dec 20, 2012 at 13:07
  • Not sure, I noticed your using <%: syntax I take it your running MVC 2? Commented Dec 20, 2012 at 13:09
  • Is your SurveyQuestionEditModel class properties read only? Commented Dec 20, 2012 at 13:12
  • Does the view "Edit" exist ? Commented Dec 20, 2012 at 13:15

1 Answer 1

3

Looks like this question: Model is null when form submitted

Replace your parameter name here:

public ActionResult EditQuestion(SurveyQuestionEditModel question)
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.