简体   繁体   English

ASP.Net MVC模型绑定我做错了什么?

[英]ASP.Net MVC Model Binding What am I doing Wrong?

I am getting a NullReference when I try to access the posted model. 当我尝试访问发布的模型时,我得到一个NullReference。 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: 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; }
    }

Looks like this question: Model is null when form submitted 看起来像这个问题: 表单提交时模型为null

Replace your parameter name here: 在此处替换您的参数名称:

public ActionResult EditQuestion(SurveyQuestionEditModel question)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM