2013-03-10 35 views
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}); 
     } 

的問題是創建方法作品的權利。它獲取參數並將其添加到數據庫。但類似的方法CreateQuestion顯示有關問題的消息爲null。 我錯了什麼?

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> 
+0

你可以發佈createquestion視圖嗎? – 2013-03-10 19:17:07

+0

我已經添加了它.. – 2013-03-10 19:25:37

+0

它抱怨Model.Question爲空?正確? – 2013-03-10 19:29:40

回答

0

這是很難弄清楚不知道該模型。別人可能會提供一個更好的答案,但這裏是我現在想到的唯一的事情:

如果你的問題的模型是這樣的:

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

你可以做什麼,現在,是ALTER您的控制器接受個人參數並自行創建對象。這可能會幫助您找出模型中哪些關鍵屬性缺失。

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對象。

對不起,我幫不了你。

祝你好運。