Post参数为空
我有以下方法的控制器:Post参数为空,post,parameters,null,Post,Parameters,Null,我有以下方法的控制器: public ActionResult Create() { return View(); } [Authorize] [HttpPost] public ActionResult Create(Tests test) { test.CreateDate = DateTime.Now; test.Au
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显示关于question为null的消息。
我错了什么
创建问题视图
<%@ 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>
Создать вопрос
Создать вопрос
Вопрос
型号类型,“ааааааааа”)百分比>
模型.类型,问题类型)%>
型号.类型)%>
型号.订单号)%>
型号.订单号)%>
--%>
模型问题,“Пццццца”)百分比>
模型问题,2,47,“””>
范本(问题)%>
model.idTest)%%>
model.idTest)%%>
model.OrderNumber)%%>
型号.订单号)%>
如果不了解模型,很难理解这一点。其他人可能会提供更好的答案,但我现在唯一能想到的是:
如果您的问题模型如下所示:
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通常足够聪明,可以将表单(视图)中的各个值绑定到模型中,但缺少一些关键值并导致问题。一旦你弄明白了这是什么,你就可以把你的控制器恢复到只接受一个问题对象
对不起,我帮不了你更多
祝你好运。你能发布createquestion视图吗?它抱怨Model.Question为空?对吗?对。Erorr消息:值不能为空。参数名称:entityHmm,听起来实际上不同。请张贴您的模型以供提问。具体是什么?它大约有300行代码,是通过创建ADO.NET实体模型自动生成的