如何从不同的ViewModel返回值?

时间:2014-01-13 16:04:42

标签: c# asp.net-mvc controller viewmodel

我试图用一个View来做这个,也许这不是最好的方法?

我有一个数据驱动的表单;这些问题都来自问题表,但答案填充了一个Response表。这是视图;

    @using Microsoft.AspNet.Identity
    @model Template.Models.Question

 @{
   ViewBag.Title = "View question";
   var qtype = Model.QuestionTypeId;
   ViewBag.Number = Model.Id - 7;
   Html.BeginForm("ViewQuestion", "Question", FormMethod.Post, new { @class = "form-horizontal", role = "form" }); 
}

<h4>Question #@ViewBag.Number</h4>
<hr />
<h1> @Model.Question1</h1>

@Html.AntiForgeryToken()
<div>

    @switch (qtype)
    {
        case 1:
            // Textbox
            @Html.TextArea("Answer", new { @class = "form-control", rows = "4", col = "5" });<br /><br />
            break;
        case 2:
            // Dropdown
            <select class="form-control" id="Answer">
            @foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
             {
                 <option value="@item.QuestionOption1">@item.QuestionOption1</option>

             }
             </select><br /><br />
             break;
        case 3:
             //  Checkbox
                 <div class="checkbox">
                 @foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
                  {
                   <input type="checkbox" name="Answer" value="@item.QuestionOption1" />  @item.QuestionOption1 <br />
                  }
                 </div><br /><br />
                 break;
        case 4:
             //      Radio buttons
             foreach (var item in Model.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
             {
              <div class="radio">
               <label>
               <input type="radio" name="Answer" value="@item.QuestionOption1" />
               @item.QuestionOption1
               </label>
               </div>
             }<br /><br />
            break;
    }

</div>
<input type="hidden" name="QuestionId" value="@Model.Id" />
<input type="hidden" name="UserId" value="@User.Identity.GetUserId()" />
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Answer" />
    </div>
</div>

这是我的ViewModel;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Template.Models
{
public class GetQuestionViewModel
{

    public int Id { get; set; }

    public int PageNumber { get; set; }

    public string Question1 { get; set; }

    public int QuestionTypeId { get; set; }

    public Nullable<int> LinkedTo { get; set; }

    public Nullable<int> Options { get; set; }

    public Nullable<int> QuestionRanking { get; set; }

    public virtual ICollection<QuestionOption> QuestionOptions { get; set; }

    public virtual QuestionType QuestionType { get; set; }

    public virtual ICollection<Response> Responses { get; set; }

    public virtual ICollection<AspNetUser> AspNetUsers { get; set; }
}

public class ResponseViewModel
{
    [Required]
    public string UserId { get; set; }

    [Required]
    public int QuestionId { get; set; }

    [Required(ErrorMessage = "Please answer the question before submitting")]
    public string Answer { get; set; }

    [Required]
    public string Source { get; set; }

    [Required]
    public string Status { get; set; }

    [Required]
    public System.DateTime DateStamp { get; set; }

    public Nullable<int> Duplicate { get; set; }

    public virtual Question Questions { get; set; }

    public object SelectedValue { get; set; }

    public virtual ICollection<QuestionOption> QuestionOptions { get; set; }
}
}

我无法让GetQuestionViewModel工作,并且已经使用问题模型的.emdx版本来填充页面,但我的任务是a)在页面上显示每个用户的答案,以及b)显示问题PageNumber字段可以在一个屏幕上显示任意数量的问题。

这是我的控制器,但你可以看到更多被评论出来而不是工作!

using System.Web.Mvc;
using Template.Models;

namespace Template.Controllers
{
public class QuestionController : Controller
{
    private WebTemplateEntities db = new WebTemplateEntities();

    // GET: /Questions/ViewQuestion/5
    public ActionResult ViewQuestion(int? id)
    {
        if (id == null || id == 0 || id > 12)
        {
            id = 8;
        }
        var question = db.Questions.Find(id);
        if (question == null)
        {
            return HttpNotFound();
        }

        return View(question);
    }

    //        [Route("Questions/{page?}")]
    //public ActionResult ViewQuestion(GetQuestionViewModel model, int? id)
    //{
    //    if (id == null || id == 0 || id > 12)
    //    {
    //        //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    //        id = 8;
    //        //return RedirectToAction("/ViewQuestion/" + id);
    //    }

    //    var q = db.Questions.Find(id);
    //    var pageId = q.PageNumber;

    //    var questions = from q in db.Questions
    //                    where q.PageNumber == pageId
    //                    orderby q.QuestionRanking
    //                    select q;
    //    return View(questions);
    // }

    // POST: /Questions/ViewQuestion/5
    [HttpPost]
    public ActionResult ViewQuestion([Bind(Include = "QuestionId, Answer, UserId")] ResponseViewModel responseViewModel)
    {
        Response re = new Models.Response();
        re.Answer = responseViewModel.Answer;
        if (re.Answer == null)
        {
            re.Answer = "Work in progress!";
            //    re.Answer = responseViewModel.SelectedValue();
            //    re.Answer = int.Parse(SelectList["Question.QuestionOption1"]);
        }
        re.UserId = responseViewModel.UserId;
        re.QuestionId = responseViewModel.QuestionId;
        var id = responseViewModel.QuestionId;
        re.Source = "Web";
        re.Status = "New";
        re.DateStamp = System.DateTime.Now;
        db.Responses.Add(re);
        db.SaveChanges();

        return RedirectToAction("ViewQuestion/" + (id + 1));
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
  }
}

我一直在研究这个问题几天,现在我只是绕圈子。任何帮助或建议都非常感谢。

1 个答案:

答案 0 :(得分:0)

看起来你混淆了实体模型和ViewModel。在您的视图中,您引用了Template.Models.Question,它似乎是一个实体而不是一个ViewModel。

视图应仅引用您的ViewModel,而不是您的实体。并且应该在您的实体和ViewModel之间建立一些映射机制。我注意到的另一件事是您的Entity和ViewModel似乎位于应用程序的同一名称空间中。这不一定是个问题,但可能会让人感到困惑,因为ViewModel仅适用于您的视图,而实体属于您的持久层。

目前,您的代码正在通过此代码获取问题对象:from q in db.Questions然后您将其传递给View(问题)。因此,您实际上不会在视图中使用ViewModel,它将是您的实体模型。

将您的观点更改为:

@model Template.Models.GetQuestionViewModel

将控制器更改为:

var questions = (from q in db.Questions
                where q.PageNumber == pageId
                orderby q.QuestionRanking
                select q.ToViewModelExtensionMethod()).ToList();

return View(questions);

您必须编写该扩展方法来执行从Template.Model.Question到Template.Model.GetQuestionViewModel的映射。

希望这有帮助。

<强>更新

有关扩展方法的信息,请参阅MSDN article

我只是使用扩展方法,因为我喜欢语法流的方式。这是一个简单的例子:

namespace Template.Models.Extensions
{
    public static class QuestionExtensions
    {
        public static GetQuestionViewModel ToViewModelExtensionMethod(this Question question)
        {
            var result = new GetQuestionViewModel();
            result.field1 = question.field1;
            // ... etc
            return result;
        }
    }   
}

另外,请注意上面的更改,我现在在查询项目时对其进行映射。