MVC Model Nullable <short> View in View </short>

时间:2014-05-07 15:41:44

标签: c# asp.net-mvc forms

我想知道是否有人能够对我得到的错误有所了解,即:

  

&#34;传入字典的模型项的类型为&System; System.Data.Entity.Infrastructure.DbQuery`1 [WebApplication1.Models.Weight]&#39;,但这个字典需要一个模型类型&#39; WebApplication1.Models.AddWeightModel&#39;。&#34;

AddWeightModel

public class AddWeightModel
{
    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Stone")]
    public Nullable<short> Stone { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Pound")]
    public Nullable<short> Pound { get; set; }        
}

Weight.cs(创建我的ADO.NET实体数据模型)

public partial class Weight
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public Nullable<short> Stone { get; set; }
    public Nullable<short> Pound { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
}

的AccountController

public ActionResult RecordCard()
{
    var weightModel = from m in db.Weights where select m;
    return View(weightModel);
}

RecordCard

@using Microsoft.AspNet.Identity;
@using Shedtember.Models;
@using System.Text;

@model IEnumerable<Shedtember.Models.Weight>

@{
    ViewBag.Title = "Record Card";

    var message = new StringBuilder();
    message.Append("data: [");

    foreach (var item in Model)
    {
        message.Append("[Date.UTC(" + @item.Date.Value.ToString("yyyy") + ", " + @item.Date.Value.ToString("mm") + ", " + @item.Date.Value.ToString("dd") + "), " + @item.Stone + "." + @item.Pound + "],");
    }
    message.Append("]");
    message.Replace("],]", "]]");
}
<div class="row">
    <article class="span7">
        <section class="block-indent-1 maxheight">
            <h2>@ViewBag.Title</h2>
            <p>Your progress so far.</p>
            <div id="container" style="min-width: 280px; height: 400px; margin-left:0px"></div>
        </section>
    </article>
    <article class="span5">
        <section class="block-indent-1 divider-left maxheight">
            <h2>Update Profile</h2>
            <p class="text-success">@ViewBag.StatusMessage</p>
            <div>
                @Html.Partial("_WeightPartial")
            </div>
        </section>
    </article>
</div>

看来该视图正在尝试接收与模型中指定的数据不同的数据,但这是不正确的,除非我错过了某些内容。

2 个答案:

答案 0 :(得分:3)

您将错误的模型类型传递给视图。您的观看次数预计为AddWeightModel,但您需要从linq查询中调整Weight。我假设你打算做这样的事情:

public ActionResult WeightCard()
{
   var weightModel = (from m in db.Weights select m).First();
   var viewModel = new AddWeightModel
   {
        Stone = weightModel.Stone,
        Pound = weightModel.Pount
   };
   return View(viewModel);
} 

编辑:如果您尝试将多个项目传递到视图,则可以更改要使用的视图:

@model IEnumerable<WebApplication1.Models.AddWeightModel>

然后传递一个集合:

var viewModel = (from m in db.Weights select new AddWeightModel
   {
        Stone = m.Stone,
        Pound = m.Pount
   }).ToList();
return View(weightModel);

请注意,代码段未经测试,您需要更改视图逻辑以迭代集合。

答案 1 :(得分:-1)

如果在视图中需要多行,则需要使用ToList()执行查询,如果在视图中需要一个对象,则使用FirstOrDefault()

var weightModel = (from m in db.Weights
                   select m).AsEnumerable().FirstOrDefault();
    return View(weightModel);

查看:

@model WebApplication1.Models.Weight

对于多个对象:

var weightModel = (from m in db.Weights
                       select m).ToList();
        return View(weightModel);

查看:

@model List<WebApplication1.Models.Weight>

如果您正在使用AddWeightModel,那么:

(from m in db.Weights select new AddWeightModel
   {
        Stone = m.Stone,
        Pound = m.Pount
   }).ToList();

查看:

@model List<WebApplication1.Models.AddWeightModel>