0

我有以下方法的控制器:

  public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(Tests test)
        {
            test.CreateDate = DateTime.Now;
            test.Author = User.Identity.Name;
            TestEntities db = new TestEntities();
            db.AddToTests(test);
            db.SaveChanges();
            return RedirectToAction("CreateQuestion", new { OrderNumber = 1, idTest = test.id });
        }

        [Authorize]
        public ActionResult CreateQuestion(int OrderNumber,int idTest)
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateQuestion(Questions question)
        {
            TestEntities db = new TestEntities();
            db.AddToQuestions(question);
            db.SaveChanges();
            return RedirectToAction("CreateQuestion", new {id = question.id, t = question.Type});
        }

问题是 Create 方法工作正常。它获取参数并将其添加到数据库。但类似的方法 CreateQuestion 显示有关问题的消息为空。我错了什么?

创建问题视图

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<test.su.Models.Questions>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Создать вопрос
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Создать вопрос</h2>

<% using (Html.BeginForm("CreateQuestion","Test")) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Вопрос</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Type,"Тип вопроса") %>
        </div>

        <% // List of question types
            List<SelectListItem> QuestionTypes = new List<SelectListItem>();
            SelectListItem t = new SelectListItem();
            t.Text = "Вопрос с вариантами ответа (флажки или радиокнопки)";
            t.Value = "0";
            QuestionTypes.Add(t);
            t = new SelectListItem();
            t.Text = "Вопрос со свободным ответом (текстовое поле)";
            t.Value = "1";
            QuestionTypes.Add(t);
             %>


        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.Type, QuestionTypes) %>
            <%: Html.ValidationMessageFor(model => model.Type) %>
        </div>

<%--        <div class="editor-label">
            <%: Html.LabelFor(model => model.OrderNumber,"Порядковый номер вопроса") %>
            <%: Html.EditorFor(model => model.OrderNumber) %>
            <%: Html.ValidationMessageFor(model => model.OrderNumber) %>
        </div>--%>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Question,"Текст вопроса") %>
        </div>
        <div class="editor-field">
            <%: Html.TextAreaFor(model => model.Question,2,47,"") %> 
            <%: Html.ValidationMessageFor(model => model.Question) %>
        </div>
        <%: Html.HiddenFor(model => model.idTest) %>
        <%: Html.ValidationMessageFor(model => model.idTest) %>
        <%: Html.HiddenFor(model => model.OrderNumber ) %>
        <%: Html.ValidationMessageFor( model => model.OrderNumber) %>
        <p>
            <input type="submit" value="Далее" />
        </p>
    </fieldset>
<% } %>
</asp:Content>
4

1 回答 1

0

这在不知道模型的情况下很难弄清楚。其他人可能会提供更好的答案,但这是我现在唯一能想到的:

如果您的问题模型如下所示:

public class Questions
{
  int Id {get;set;}
  string Name {get;set;}
  string Description {get;set;}
}

现在,您可以做的是更改您的控制器以接受各个参数并自己创建对象。这可能会帮助您确定模型中缺少哪些关键属性。

public ActionResult CreateQuestion(string Name, string Description)
{
  //make the entity yourself
  Questions newQuestion = new Questions()
  {
    Name = Name,
    Description = Description
  }
  //your other code here
}

现在通常 MVC 足够聪明,可以将表单(视图)中的各个值绑定到模型,但是缺少一些关键值并导致您出现问题。一旦你弄清楚那是什么,你实际上可以将你的控制器恢复为只接受一个 Questions 对象。

对不起,我不能帮助你更多。

祝你好运。

于 2013-03-10T20:15:52.307 回答