如何绑定模型从get到post方法MVC 4

时间:2015-04-09 16:43:12

标签: c# asp.net-mvc-4

我目前陷入了实施的某个阶段,我使用ViewModel类来显示数据,但是我需要在ViewModel中发布来自其他对象的值等于给定值。这是两个模型类

ViewModel.cs

public class ViewModel
{
    public IList<Answer> Answers { get; set; }
    public Question Questions { get; set; }

}

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; }
}

到目前为止一切顺利。我正在使用问题和答案列表的对象来显示我在控制器中需要的数据

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

    @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" />

}

现在,在我的帖子方法中,我需要获取答案列表的vm.Question.QuestionIdAnswerId的值,将它们设置为等于UserScore.Q_IdUserScore.A_Id。我怎样才能做到这一点 ?我尝试了很多方法但没有成功。

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);
    }

一般情况下,我需要知道如何将此vm.something绑定到us.something,因为问题似乎始终为空。

3 个答案:

答案 0 :(得分:2)

vm.Questions.QuestionID返回NULL,因为你没有在任何地方使用过那个。一个简单的黑客就是使用隐藏字段来捕获值,或者你应该再次初始化你的viewmodel并遵循post方法中的逻辑。

答案 1 :(得分:1)

这些值为null,因为它们不在您的视图中。您需要以隐藏控件的形式将它们保存在View中。您在帖子中收到的ViewModel只能使用View中存在的值构建ViewModel。由于View中没有维护ID,因此构造的ViewModel具有空ID。

您可以使用

@Html.HiddenFor(model => model.Questions.ID) 

以及您的回答ID

@Html.HiddenFor(m => m.Answers[i].ID)

答案 2 :(得分:1)

你应该在视图中使用hiddenfor帮助器并尝试类似的东西。

@Html.hiddenfor(m=>vm.Questions.QuestionId)

试一试:Html.HiddenFor value property not getting set