[英]How to bind Model from get to post method MVC 4
I am currently stuck at some point of my implementation where I use a ViewModel class to display data, but I need to post values from other object which are equal to given values in the ViewModel. 我目前在实现过程中停留在某个地方,我使用ViewModel类来显示数据,但是我需要发布来自其他对象的值,这些值等于ViewModel中的给定值。 Here are both model classes
这是两个模型类
ViewModel.cs ViewModel.cs
public class ViewModel
{
public IList<Answer> Answers { get; set; }
public Question Questions { get; set; }
}
UserScore.cs UserScore.cs
public partial class UserScore
{
public int ScoreID { get; set; }
public int U_Id { get; set; }
public int A_Id { get; set; }
public bool CorrectAnswer { get; set; }
public int Q_Id { get; set; }
public virtual Answer Answer { get; set; }
public virtual Member Member { get; set; }
public virtual Question Question { get; set; }
}
So far so good. 到现在为止还挺好。 I am using an object of Question and List of Answers to display the data I need in my Controller
我正在使用“问题和答案列表”对象来显示我的控制器中所需的数据
Controller.cs Controller.cs
public ActionResult TakeTest(int id=0)
{
ViewModel vm = new ViewModel();
Test t = db.Tests.Find(id);
if (t == null)
{
return HttpNotFound();
}
vm.Questions = (from q in db.Questions
join tt in db.Tests on q.BelongToTest equals tt.TestId
where q.BelongToTest == id
select q).FirstOrDefault();
vm.Answers = new List<Answer>(from a in db.Answers
join q in db.Questions on a.BelongToQuestion equals q.QuestionId
join tt in db.Tests on q.BelongToTest equals tt.TestId
where q.BelongToTest == id &&
a.BelongToQuestion == vm.Questions.QuestionId
select a).ToList();
foreach (var i in vm.Answers)
{
i.CorrectOrNot = false;
}
return View(vm);
}
View.cshtml View.cshtml
@model MvcTestApplication.Models.ViewModel
@using MvcTestApplication.Models
@{
ViewBag.Title = "TakeTest";
}
<h2>TakeTest</h2>
@using (Html.BeginForm()) {
<table>
<tr>
<th>Question Name</th>
</tr>
<tr>
<td>@Html.DisplayFor(model => model.Questions.Question_Text)</td>
</tr>
</table>
<table id="dataTable">
<tr>
<th>Correct?</th>
<th>Answer text</th>
<th>Open Answer</th>
</tr>
@for(int i = 0; i < Model.Answers.Count; i++)
{
<tr>
<td>@Html.CheckBoxFor(m => m.Answers[i].CorrectOrNot)</td>
<td>@Html.DisplayFor(m => m.Answers[i].AnswerText)</td>
<td>@Html.EditorFor(m => m.Answers[i].OpenAnswerText)</td>
</tr>
}
</table>
if(ViewBag.Message != null)
{
<script>
$(document).ready(function(){
alert('@ViewBag.Message');
});
</script>
}
<input type="submit" value="Next Question" />
}
Now in my post method I need to get the value of vm.Question.QuestionId
and AnswerId
of the Answer list, set them to be equal to UserScore.Q_Id
and UserScore.A_Id
. 现在,在我的post方法中,我需要获取Answer列表的
vm.Question.QuestionId
和AnswerId
的值,将它们设置为等于UserScore.Q_Id
和UserScore.A_Id
。 How can I do that ? 我怎样才能做到这一点 ? I tried many ways but with no success.
我尝试了很多方法,但没有成功。
Controller.cs Controller.cs
[HttpPost]
[Authorize]
public ActionResult TakeTest(ViewModel vm)
{
if (ModelState.IsValid)
{
UserScore us = new UserScore();
us.U_Id = (from m in db.Members
where m.UserID == WebSecurity.CurrentUserId
select m.MemberId).FirstOrDefault();
us.A_Id = 49;
//us.A_Id = vm.Questions.QuestionID returns NULL
us.Q_Id = 150;
db.UserScores.Add(us);
db.SaveChanges();
}
return View(vm);
}
In general I need to know how to bind this vm.something
to us.something
because Question appears to be null all the time. 通常,我需要知道如何将此
vm.something
绑定到us.something
因为Question一直都是null。
vm.Questions.QuestionID returns NULL because u haven't used that on the view anywhere. vm.Questions.QuestionID返回NULL,因为您尚未在视图的任何地方使用它。 A easy hack would be to use a hidden field to capture the value or you should be initializing your viewmodel again and follow the logic in the post method.
一个简单的技巧是使用隐藏字段来捕获值,或者您应该重新初始化视图模型并遵循post方法中的逻辑。
These values are null, because they are not present in your view. 这些值是空的,因为它们在您的视图中不存在。 You will need to keep them in your View in the form of a hidden control.
您将需要以隐藏控件的形式将它们保留在视图中。 The ViewModel that you receive in the post can only construct the ViewModel using values present in the View.
您在帖子中收到的ViewModel只能使用View中存在的值构造ViewModel。 Since there is no ID maintained in the View, the constructed ViewModel has a null ID.
由于视图中没有维护ID,因此构造的ViewModel具有空ID。
You can use 您可以使用
@Html.HiddenFor(model => model.Questions.ID)
and for your answer ID 和你的答案ID
@Html.HiddenFor(m => m.Answers[i].ID)
You should use an hiddenfor helpers in your view and try something like that. 您应该在视图中使用hiddenfor帮助器,然后尝试类似的操作。
@Html.hiddenfor(m=>vm.Questions.QuestionId)
Give it a try : Html.HiddenFor value property not getting set 尝试一下: Html.HiddenFor value属性未设置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.