C# 模型绑定回发
我有两种这样的方法C# 模型绑定回发,c#,asp.net-mvc,asp.net-mvc-4,model-binding,C#,Asp.net Mvc,Asp.net Mvc 4,Model Binding,我有两种这样的方法 public ViewResult Detail(int Id) { if (Id != null) { Context context = new Context(); Poll PollDetail = context.Polls.FirstOrDefault(x => x.Id == Id); PollDetail.Answers = new List&
public ViewResult Detail(int Id)
{
if (Id != null)
{
Context context = new Context();
Poll PollDetail = context.Polls.FirstOrDefault(x => x.Id == Id);
PollDetail.Answers = new List<Answer>();
Context Context = new Context();
PollDetail.Answers = Context.Answers.Where(x => x.PollId == PollDetail.Id).ToList();
return View("../Home/Index", PollDetail);
}
RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult PollVote(Poll CurrentPoll)
{
Context Context = new Context();
foreach (Answer item in CurrentPoll.Answers)
{
item.VoteCount = item.VoteCount + 1;
}
return View();
}
公共视图结果详细信息(int-Id)
{
如果(Id!=null)
{
上下文=新上下文();
Poll PollDetail=context.Polls.FirstOrDefault(x=>x.Id==Id);
PollDetail.Answers=新列表();
上下文=新上下文();
PollDetail.Answers=Context.Answers.Where(x=>x.PollId==PollDetail.Id).ToList();
返回视图(“../Home/Index”,PollDetail);
}
重定向到操作(“索引”、“主页”);
}
[HttpPost]
公共行动结果投票(Poll CurrentPoll)
{
上下文=新上下文();
foreach(CurrentPoll.Answers中的答案项)
{
item.VoteCount=item.VoteCount+1;
}
返回视图();
}
没有cshtml。所以这一部分没有问题
<div class="container">
@Html.Partial("Header")
@if (Model == null)
{
@Html.Partial("CreatePoll")
}
else
{
using (@Html.BeginForm("PollVote", "Poll", FormMethod.Post, new { id = "PollVoteForm" }))
{
<div class="row-fluid">
<div class="span12 pageHeader">
<h2>SORU:</h2>
@Html.LabelFor(m => m.Question, Model.Question, new { @class = "question-input", @id = "question" })
</div>
</div>
<div id="answers" class="row-fluid">
@foreach (Answer answer in Model.Answers)
{
<p class="external">
@Html.RadioButtonFor(m => answer.Content, answer.Content, new { @name = "rb", @class = "answer-radio", @id = "answer-" + answer.Counter, @checked = "false" })
@Html.Label(answer.Content, new { @for = "answer-" + answer.Counter })
@Html.HiddenFor(m => answer.Content)
</p>
}
</div>
<div class="row-fluid">
<div class="span6"></div>
<div class="span5">
<input type="submit" value="Oyla" class="btnPS" />
</div>
</div>
}
}
<div class="footer"></div>
</div>
@Html.Partial(“标题”)
@if(Model==null)
{
@Html.Partial(“CreatePoll”)
}
其他的
{
使用(@Html.BeginForm(“PollVote”,“Poll”,FormMethod.Post,new{id=“PollVoteForm”}))
{
索鲁:
@LabelFor(m=>m.Question,Model.Question,new{@class=“Question input”,@id=“Question”})
@foreach(模型中的答案。答案)
{
@Html.radioButton(m=>answer.Content,answer.Content,new{@name=“rb”、@class=“answer radio”、@id=“answer-”+answer.Counter、@checked=“false”})
@Label(answer.Content,新的{@for=“answer-”+answer.Counter})
@HiddenFor(m=>answer.Content)
}
}
}
投票模型完美绑定。但我不能支持任何数据。当我在index.cshtml中提交表单时。CurrentPoll模型为空。如何修复它?Asp.Net MVC需要模型上的属性才能进行模型绑定。因此,请检查您的模型并确保所有成员都作为属性公开 你把你的模型改成下面的样子
public class Poll
{
public Answer Answers { get; set; }
}
你能显示提交到
PollVote
的表单的代码吗?你能显示index.cshtml的代码吗请在index.cshtml中显示你的代码和Poll classis的定义我做了类似于此的公开IList回答{get;set;}谢谢你的回答。我忘记了一个类中的get/set属性,这让我花了两个小时试图解决它。