【问题标题】:How to bind Model from get to post method MVC 4如何绑定模型从 get 到 post 方法 MVC 4
【发布时间】:2015-04-09 16:43:11
【问题描述】:

我目前在使用 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" />

}

现在在我的 post 方法中,我需要获取答案列表的 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,因为问题似乎一直为空。

【问题讨论】:

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


    【解决方案1】:

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

    你可以使用

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

    以及您的答案 ID

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

    【讨论】:

      【解决方案2】:

      vm.Questions.QuestionID 返回 NULL,因为你没有在任何地方的视图上使用它。一个简单的技巧是使用隐藏字段来捕获值,或者您应该再次初始化视图模型并遵循 post 方法中的逻辑。

      【讨论】:

        【解决方案3】:

        您应该在视图中使用 hiddenfor 助手并尝试类似的方法。

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

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

        【讨论】:

        • m=&gt;vm ? m ?虚拟机?
        猜你喜欢
        • 2014-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2022-12-09
        • 2013-01-27
        • 1970-01-01
        相关资源
        最近更新 更多