Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 如何将数据绑定到viewmodel中的复选框_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 如何将数据绑定到viewmodel中的复选框

Asp.net mvc 4 如何将数据绑定到viewmodel中的复选框,asp.net-mvc-4,Asp.net Mvc 4,我正在尝试创建一个视图模型来显示复选框列表。当在数据库中找到其ID时,将选中复选框。但是,我的代码生成了一个错误 检查设备 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace XNet.WebUI.Hotel.ViewModel { public class CheckFacilityVM { public int Facil

我正在尝试创建一个视图模型来显示复选框列表。当在数据库中找到其ID时,将选中复选框。但是,我的代码生成了一个错误

检查设备

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

namespace XNet.WebUI.Hotel.ViewModel
{
    public class CheckFacilityVM
    {
        public int FacilityID { get; set; }
        public string facilityName { get; set; }
        public List<FacilityAvailable> facilityAvailable { get; set; }
    }

    public class FacilityAvailable
    {
        public bool isCheck { get; set; }
    }
}

如何设置这些复选框?

首先调整视图模型:

public class CheckFacilityVM
{
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
    public bool IsFacilityAvailable { get; set; }
}
public ActionResult Facility()
{
    var model = _hotelService
        .ShowRoomFacility(2)
        .Select(htl => new CheckFacilityVM
        {
            FacilityID = html.FacilityID,
            FacilityName = html.FacilityName,
            IsFacilityAvailable = htl.IsActive,
        })
        .ToList();

    return View(model);
}
然后使用此视图模型:

public class CheckFacilityVM
{
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
    public bool IsFacilityAvailable { get; set; }
}
public ActionResult Facility()
{
    var model = _hotelService
        .ShowRoomFacility(2)
        .Select(htl => new CheckFacilityVM
        {
            FacilityID = html.FacilityID,
            FacilityName = html.FacilityName,
            IsFacilityAvailable = htl.IsActive,
        })
        .ToList();

    return View(model);
}
然后写一个相应的视图:

@model List<CheckFacilityVM>

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Is available</th>
            </tr>
        </thead>
        <tbody>
        @for (var i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(x => x[i].FacilityID)
                    @Html.HiddenFor(x => x[i].FacilityID)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].FacilityName)
                    @Html.HiddenFor(x => x[i].FacilityName)
                </td>
                <td>
                    @Html.CheckBoxFor(x => x[i].IsFacilityAvailable)
                </td>
            </tr>
        }
        </tbody>
    </table>

    <button type="submit">Save</button>
}
@型号列表
@使用(Html.BeginForm())
{
身份证件
名称
有空吗
@对于(var i=0;ix[i].FacilityID)
@Html.HiddenFor(x=>x[i].FacilityID)
@DisplayFor(x=>x[i].FacilityName)
@Html.HiddenFor(x=>x[i].FacilityName)
@Html.CheckBoxFor(x=>x[i].IsFacilityAvailable)
}
拯救
}
最后:

[HttpPost]
public ActionResult Facility(List<CheckFacilityVM> model)
{
    // process the values from the view model here ...
}
[HttpPost]
公共行动结果设施(列表模型)
{
//在此处处理视图模型中的值。。。
}

首先调整视图模型:

public class CheckFacilityVM
{
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
    public bool IsFacilityAvailable { get; set; }
}
public ActionResult Facility()
{
    var model = _hotelService
        .ShowRoomFacility(2)
        .Select(htl => new CheckFacilityVM
        {
            FacilityID = html.FacilityID,
            FacilityName = html.FacilityName,
            IsFacilityAvailable = htl.IsActive,
        })
        .ToList();

    return View(model);
}
然后使用此视图模型:

public class CheckFacilityVM
{
    public int FacilityID { get; set; }
    public string FacilityName { get; set; }
    public bool IsFacilityAvailable { get; set; }
}
public ActionResult Facility()
{
    var model = _hotelService
        .ShowRoomFacility(2)
        .Select(htl => new CheckFacilityVM
        {
            FacilityID = html.FacilityID,
            FacilityName = html.FacilityName,
            IsFacilityAvailable = htl.IsActive,
        })
        .ToList();

    return View(model);
}
然后写一个相应的视图:

@model List<CheckFacilityVM>

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Is available</th>
            </tr>
        </thead>
        <tbody>
        @for (var i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(x => x[i].FacilityID)
                    @Html.HiddenFor(x => x[i].FacilityID)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].FacilityName)
                    @Html.HiddenFor(x => x[i].FacilityName)
                </td>
                <td>
                    @Html.CheckBoxFor(x => x[i].IsFacilityAvailable)
                </td>
            </tr>
        }
        </tbody>
    </table>

    <button type="submit">Save</button>
}
@型号列表
@使用(Html.BeginForm())
{
身份证件
名称
有空吗
@对于(var i=0;ix[i].FacilityID)
@Html.HiddenFor(x=>x[i].FacilityID)
@DisplayFor(x=>x[i].FacilityName)
@Html.HiddenFor(x=>x[i].FacilityName)
@Html.CheckBoxFor(x=>x[i].IsFacilityAvailable)
}
拯救
}
最后:

[HttpPost]
public ActionResult Facility(List<CheckFacilityVM> model)
{
    // process the values from the view model here ...
}
[HttpPost]
公共行动结果设施(列表模型)
{
//在此处处理视图模型中的值。。。
}

+1击败我:)。为什么您在提交时使用了
按钮
而不是
输入
?对
int
类型也使用
var
?懒惰:P
Save
相同-这实际上是个人喜好的问题。@noviankristianto,你有什么样的错误?我看不懂你的心思,那是成功。我能处理这个错误。。我还有一个问题。当我的设备可用时,如何选中复选框????我很困惑。。。请帮帮我。。给我举个例子+1击败我:)。为什么您在提交时使用了
按钮
而不是
输入
?对
int
类型也使用
var
?懒惰:P
Save
相同-这实际上是个人喜好的问题。@noviankristianto,你有什么样的错误?我看不懂你的心思,那是成功。我能处理这个错误。。我还有一个问题。当我的设备可用时,如何选中复选框????我很困惑。。。请帮帮我。。给我举个例子