Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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
C# 从多个字段填充ViewModel集合_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 从多个字段填充ViewModel集合

C# 从多个字段填充ViewModel集合,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个表单,用户可以在其中选择他们想要为他们正在销售的产品支持的运输方式,例如一等信件、二等信件、包裹等。我只给用户提供一组可能的运输方式,他们声明每种方式的成本,因此如果有人想在包裹中销售烤面包机,他们的收费将低于一套哑铃 MyProductViewModel: public int Id { get; set; } public ICollection<SelectedShippingMethodViewModel> SelectedShippingMethods { get

我有一个表单,用户可以在其中选择他们想要为他们正在销售的产品支持的运输方式,例如一等信件、二等信件、包裹等。我只给用户提供一组可能的运输方式,他们声明每种方式的成本,因此如果有人想在包裹中销售烤面包机,他们的收费将低于一套哑铃

My
ProductViewModel

public int Id { get; set; }

public ICollection<SelectedShippingMethodViewModel> SelectedShippingMethods { get; set; }
public class SelectedShippingMethodViewModel
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}
在我的表格中,我创建了一个包含以下可能选项的部分:

<h3>Add new product</h3>
<hr />
@using (Html.BeginForm("AddNew", "ProductCreator", null, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        <label class="col-sm-2 control-label">Shipping methods</label>
        <div class="col-sm-10">
            @foreach (ShippingMethod shippingMethod in ViewBag?.ShippingMethods)
            {
                <div class="row">
                    <div class="col-md-3">
                        // I don't know what should be here
                        @Html.CheckBox("SelectedShippingMethods", false)
                        @shippingMethod.Name
                    </div>
                    <div class="col-md-2">
                        // I don't know what should be here
                        @Html.TextBox("SelectedShippingMethods.Price")
                    </div>
                </div>
            }
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Add product</button>
        </div>
    </div>
}
[HttpGet]
public async Task<ActionResult> AddNew()
{
    ViewBag.ShippingMethods = await _shippingService.GetAllShippingMethodsAsync();
    return View();
}
添加新产品

@使用(Html.BeginForm(“AddNew”,“ProductCreator”,null,FormMethod.Post,new{@class=“form horizontal”,role=“form”})) { @Html.AntiForgeryToken() 运输方式 @foreach(ShippingMethod视图包中的ShippingMethod?.ShippingMethods) { //我不知道这里应该有什么 @复选框(“SelectedShippingMethods”,false) @shippingMethod.Name //我不知道这里应该有什么 @TextBox(“SelectedShippingMethods.Price”) } 添加产品 }
我有一个数据库表,其中包含我获得的每种可能的装运方法,如下所示:

<h3>Add new product</h3>
<hr />
@using (Html.BeginForm("AddNew", "ProductCreator", null, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        <label class="col-sm-2 control-label">Shipping methods</label>
        <div class="col-sm-10">
            @foreach (ShippingMethod shippingMethod in ViewBag?.ShippingMethods)
            {
                <div class="row">
                    <div class="col-md-3">
                        // I don't know what should be here
                        @Html.CheckBox("SelectedShippingMethods", false)
                        @shippingMethod.Name
                    </div>
                    <div class="col-md-2">
                        // I don't know what should be here
                        @Html.TextBox("SelectedShippingMethods.Price")
                    </div>
                </div>
            }
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Add product</button>
        </div>
    </div>
}
[HttpGet]
public async Task<ActionResult> AddNew()
{
    ViewBag.ShippingMethods = await _shippingService.GetAllShippingMethodsAsync();
    return View();
}
[HttpGet]
公共异步任务AddNew()
{
ViewBag.ShippingMethods=await_shippingService.GetAllShippingMethodsAsync();
返回视图();
}

问题是如果选中复选框,我必须为每个选中的
ShippingMethodViewModel
绑定
Price
Name
,我不知道如何使其工作。

您的视图模型不正确。为了允许用户选择他们想要的运输方式并添加价格,需要对该视图模型进行修改

public class ShippingMethodViewModel
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public bool IsSelected { get; set; } // your checkbox binds to this property
}
ProductViewModel
应该是

public class ProductViewModel
{
    public int Id { get; set; }
    ....
    public List<ShippingMethodViewModel> ShippingMethods { get; set; }
}
在视图中,使用
for
循环或
EditorTemplate
for type of
ShippingMethodViewModel
正确生成表单控件

@for (int i = 0; i < Model.ShippingMethods.Count; i++)
{
    @Html.LabelFor(m => m.ShippingMethods[i].IsSelected, Model[0].ShippingMethods.Name)  
    @Html.CheckBoxFor(m => m.ShippingMethods[i].IsSelected)

    @Html.LabelFor(m => m.ShippingMethods[i].Price)       
    @Html.TextBoxFor(m => m.ShippingMethods[i].Price)

    @Html.HiddenFor(m => m.ShippingMethods[i].Name) // if you want this to be submitted as well
}

您不能将复选框绑定到
SelectedShippingMethodViewModel
的集合,这就是
SelectedShippingMethods
。您的文本框也无法绑定到模型中的任何内容。现在还不清楚你想在这里做什么-这个表单发布的方法是什么?使用
for
循环而不是
foreach
。否则,表单控件将无法使用正确的
name
属性创建。可能重复@adiga,我想绑定3个字段,而不仅仅是一个复选框。复选框实际上只是忽略未选中的装运方式。我可以在我的模型中添加额外的
bool
字段,但是绑定名称和价格的主要问题仍然存在。不管是3个属性还是10个属性。只需在循环中为
Price
属性添加一个
TextBoxfor()
。谢谢,我没有想到在
Get
中设置名称。我还假设
ShippingMethods
将有一个
Id
(PK)属性,您希望将其作为隐藏输入包含在视图模型和视图中。我还回滚了您的编辑(请不要更改原始问题,因为它会使添加的注释和答案无效)。谢谢,我想通过删除问题中的误导性句子来澄清我的问题。