c# - MVC @Html.Partial - 两种模型

标签 c# asp.net-mvc-3 razor telerik-mvc

我正在尝试在 View 中使用部分 View 。第一个 View 需要一个模型,而部分 View 需要相同模型的 IEnumerable。我收到以下错误:

The model item passed into the dictionary is of type 'MyVDC.Models.LogBook', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MyVDC.Models.LogBook]'.

在 Controller 中:

public ActionResult Create(string phn)
    {
        phn = MySession.Current.PHN;
        ViewBag.PHN = MySession.Current.PHN;
        LogBook logBook = new LogBook();
        try
        {
            logBook = db.LogBooks.Where(c => c.PHN == phn).OrderByDescending(x => x.Day).First();
        }
        catch
        {
            logBook.Day = DateTime.Now.Date;
            logBook.PHN = phn;
        }
        return View(logBook);
    }

局部 View 的第二个 Action :

 public ActionResult Grid()
        {
            string phn = MySession.Current.PHN;
            return View(db.LogBooks.ToList().Where(c => c.PHN == phn));
        }

第一个 View :

@model MyVDC.Models.LogBook
@{

}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <input type="hidden" name="PHN" id="PHN" value="@ViewBag.PHN" />
    <fieldset>
        <legend>LogBook</legend>
        @(Html.Telerik().DatePicker()
            .Name("Day")

    )
        <p>
            <input type="submit" value="Start New Logbook" class="t-button" />
        </p>
    </fieldset>
}
<div>
</div>
     @Html.Partial("Grid")

第二种观点:

@model IEnumerable<MyVDC.Models.LogBook>

@(Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
        {

            columns.Bound(o => o.Day).Format("{0:MM/dd/yyyy}").Width(120);
        })
        .DataBinding(dataBinding => dataBinding.Ajax().Select("Grid", "LogBook"))
        .Pageable()
        .Sortable()
        .Filterable()
)

非常感谢您的建议。求解,见下文。

最佳答案

我想通了,通过使用 ViewData 如下: 在 Controller 中:

   ViewData["Log"] = db.LogBooks.ToList().Where(c => c.PHN == phn);

在 View 中:

@(Html.Telerik().Grid((IEnumerable<MyVDC.Models.LogBook>)ViewData["Log"])

希望这对某人有帮助。

关于c# - MVC @Html.Partial - 两种模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795357/

相关文章:

performance - asp.net MVC3 中输出缓存的替代缓存解决方案有哪些?

jquery - 如何将 ViewBag 值传递给 razor 中的 JavaScript 函数?

javascript - 动态追加 div 和类

c# - 使用 ClickOnce 安装程序进行用户验证?

c# - WCF 单例服务错误 : The service type provided could not be loaded as a service because it does not have a default constructor

jquery - JS int 数组到 MVC3 Controller

javascript - 在 MVC Javascript 函数中,使用 Ajax.ActionLink 不会触发

c# - 在页面构造函数中异步调用 Web 服务

c# - .NET 运行时错误导致进程崩溃!但是没有未处理的异常 - 为什么? CLR 错误?

asp.net-mvc-3 - MvcContrib.Mvc3-ci 3.0.75.0 重大变化?