数据没有得到必将在操作方法 [英] Data not getting bound in action method

查看:102
本文介绍了数据没有得到必将在操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的pubs数据库,基本上我展示一个页面上作者的列表,并有编辑链接。在编辑链接的点击,一页被显示,其中有作者的信息,并用的CheckBoxList除了这个特定的作者写的标题对号。

这是我的code到现在:

HomeController的行动方式:

 公众的ActionResult编辑(字符串ID)
{
    作者author = db.authors.Include(titleauthors),其中(A => a.au_id == ID)。单();    清单< TitleViewModel> titleViewModelList = db.tit​​les.Select<标题,TitleViewModel>(标题=>
        新TitleViewModel {=器isChecked假的,标题= title.title1,title_id的= title.title_id})了ToList()。    的foreach(在author.titleauthors VAR项)
    {
        TitleViewModel titleViewModel = titleViewModelList.Where(型号=> model.title_id == item.title_id).SingleOrDefault();
        如果(titleViewModel!= NULL)
            titleViewModel.IsChecked = TRUE;
    }    AuthorViewModel AuthorViewModel =新AuthorViewModel
    {
        作者=作者,
        TitleViewModels = titleViewModelList
    };    返回查看(AuthorViewModel);
}[HttpPost]
公众的ActionResult编辑(AuthorViewModel AuthorViewModel)
{    返回RedirectToAction(「指数」);
}

Edit.cshtml:

  @model MVCCheckboxList.ViewModels.AuthorViewModel
@ {
    ViewBag.Title =编辑;
    布局=〜/查看/共享/ _Layout.cshtml
}
< H2>
    编辑< / H>
<&字段集GT;
    @using(Html.BeginForm())
    {
        <传奇>编辑作者和LT; /传说>
        <表>
            &所述; TR>
                &所述; TD>
                    @ Html.LabelFor(型号=> model.Author.au_fname)
                < / TD>
                &所述; TD>
                    @ Html.TextBoxFor(型号=> model.Author.au_fname)
                < / TD>
            < / TR>
            &所述; TR>
                &所述; TD>
                    @ Html.LabelFor(型号=> model.Author.au_lname)
                < / TD>
                &所述; TD>
                    @ Html.TextBoxFor(型号=> model.Author.au_lname)
                < / TD>
            < / TR>
            &所述; TR>
                &所述; TD>
                    @ Html.LabelFor(型号=> model.Author.address)
                < / TD>
                &所述; TD>
                    @ Html.TextBoxFor(型号=> model.Author.address)
                < / TD>
            < / TR>
            &所述; TR>
                < TD VALIGN =顶>
                    标题
                < / TD>
                &所述; TD>
                    @ Html.EditorFor(型号=> model.TitleViewModels,TitleViewModels)
                < / TD>
            < / TR>
            &所述; TR>
                &所述; TD>
                    &所述p为H.;
                        <输入类型=提交值=保存/>
                    &所述; / P>
                < / TD>
            < / TR>
        < /表>
    }
< /字段集>

这是我的AuthorViewModel:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间MVCCheckboxList.ViewModels
{
    公共类AuthorViewModel
    {
        公开名单< TitleViewModel> TitleViewModels {搞定;组; }
        公共作者作者{搞定;组; }
    }
}

这是我TitleViewModel:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;命名空间MVCCheckboxList.ViewModels
{
    公共类TitleViewModel
    {
        公共字符串的title_id {搞定;组; }
        公共BOOL {器isChecked获得;组; }
        公共字符串名称{搞定;组; }
    }
}

信息才能正确显示在编辑页面。但是,当我点击提交,在AuthorViewModel作者属性正确地保存所有数据,但列表< TitleViewModel> AuthorViewModel 为空。任何人都可以看出错误,为什么会出现这种情况?

这是我的EditorTemplate( TitleViewModels.cshtml

  @model IEnumerable的< MVCCheckboxList.ViewModels.TitleViewModel>
<表>@foreach(以型号VAR项){
    &所述; TR>
        &所述; TD>
            @ Html.HiddenFor(modelItem => item.title_id)
        < / TD>
        &所述; TD>
            @ Html.CheckBoxFor(modelItem => item.IsChecked)
        < / TD>
        &所述; TD>
            @ Html.DisplayFor(modelItem => item.Title)
        < / TD>
    < / TR>
}< /表>


解决方案

在这里你找到列表更多的信息绑定。

只要改变你的EditorTemplate的名称 TitleViewModel.cshtml 并粘贴code:

  @model MVCCheckboxList.ViewModels.TitleViewModel&所述; TD>
    @ Html.HiddenFor(modelItem => Model.title_id)
< / TD>
&所述; TD>
    @ Html.CheckBoxFor(modelItem => Model.IsChecked)
< / TD>
&所述; TD>
    @ Html.DisplayFor(modelItem => Model.Title)
< / TD>

Edit.cshtml 使用下面code,从列表生成每个元素编辑

 <表>
@for(INT I = 0; I< Model.TitleViewModels.Count;我++){
    @ Html.EditorFor(M = GT; Model.TitleViewModels [I])
}
< /表>

I am using pubs database and basically I show a list of authors on one page and there is edit link. On click of edit link, one page gets showed which has author's information and checkboxlist with checkmark besides the titles that the particular author have written.

This is my code till now:

Action methods of HomeController:

public ActionResult Edit(string id)
{
    author author = db.authors.Include("titleauthors").Where(a => a.au_id == id).Single();

    List<TitleViewModel> titleViewModelList = db.titles.Select<title, TitleViewModel>(title =>
        new TitleViewModel { IsChecked = false, Title = title.title1, title_id = title.title_id }).ToList();

    foreach (var item in author.titleauthors)
    {
        TitleViewModel titleViewModel = titleViewModelList.Where(model => model.title_id == item.title_id).SingleOrDefault();
        if (titleViewModel != null)
            titleViewModel.IsChecked = true;
    }

    AuthorViewModel AuthorViewModel = new AuthorViewModel
    {
        Author = author,
        TitleViewModels = titleViewModelList
    };

    return View(AuthorViewModel);
}

[HttpPost]
public ActionResult Edit(AuthorViewModel AuthorViewModel)
{

    return RedirectToAction("Index");
}

Edit.cshtml:

@model MVCCheckboxList.ViewModels.AuthorViewModel
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Edit</h2>
<fieldset>
    @using (Html.BeginForm())
    {
        <legend>Edit author</legend>
        <table>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.au_fname)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.au_fname)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.au_lname)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.au_lname)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Author.address)
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Author.address)
                </td>
            </tr>
            <tr>
                <td valign="top">
                    Titles
                </td>
                <td>
                    @Html.EditorFor(model => model.TitleViewModels, "TitleViewModels")
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <input type="submit" value="Save" />
                    </p>
                </td>
            </tr>
        </table>
    }
</fieldset>

This is my AuthorViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCheckboxList.ViewModels
{
    public class AuthorViewModel
    {
        public List<TitleViewModel> TitleViewModels { get; set; }
        public author Author { get; set; }
    }
}

and this is my TitleViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCheckboxList.ViewModels
{
    public class TitleViewModel
    {
        public string title_id { get; set; }
        public bool IsChecked { get; set; }
        public string Title { get; set; }
    }
}

The information gets properly displayed in Edit page. But when I click on submit, the Author property in AuthorViewModel correctly holds all the data but the List<TitleViewModel> in AuthorViewModel is null. Can anybody spot the mistake why is this happening?

This is my EditorTemplate(TitleViewModels.cshtml):

@model IEnumerable<MVCCheckboxList.ViewModels.TitleViewModel>
<table>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.HiddenFor(modelItem => item.title_id)
        </td>
        <td>
            @Html.CheckBoxFor(modelItem => item.IsChecked)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
    </tr>
}

</table>

解决方案

Here you find more informations about list binding.

Just change the name of your EditorTemplate to TitleViewModel.cshtml and paste that code:

@model MVCCheckboxList.ViewModels.TitleViewModel

<td>
    @Html.HiddenFor(modelItem => Model.title_id)
</td>
<td>
    @Html.CheckBoxFor(modelItem => Model.IsChecked)
</td>
<td>
    @Html.DisplayFor(modelItem => Model.Title)
</td>

In Edit.cshtml use below code to generate editor for each element from your list

<table>
@for (int i = 0; i < Model.TitleViewModels.Count; i++) {
    @Html.EditorFor(m => Model.TitleViewModels[i])
}
</table>

这篇关于数据没有得到必将在操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆