C# 如何为列表类型实体编写Razor代码?
我的视图模型是:C# 如何为列表类型实体编写Razor代码?,c#,asp.net-mvc,C#,Asp.net Mvc,我的视图模型是: public class CompanyAccountViewModel { public string Name { get; set; } public float Interval { get; set; } public List<string> MobileNo { get; set; } } 公共类公司帐户视图模型 { 公共字符串名称{get;set;} 公共浮点间隔{get;set;} 公共列表MobileNo{get;
public class CompanyAccountViewModel
{
public string Name { get; set; }
public float Interval { get; set; }
public List<string> MobileNo { get; set; }
}
公共类公司帐户视图模型
{
公共字符串名称{get;set;}
公共浮点间隔{get;set;}
公共列表MobileNo{get;set;}
}
我的看法是:
@model PowerSupply.Models.CompanyAccountViewModel
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Register</h3> <br />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group row">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.Interval, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3 positioned_relative">
@Html.EditorFor(model => model.Interval, new { htmlAttributes = new { @class = "form-control" } })
<span class="help-text">Hour</span>
@Html.ValidationMessageFor(model => model.Interval, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
@Html.LabelFor(model => model.MobileNo, htmlAttributes: new { @class = "col-sm-2 col-md-1 col-form-label" })
<div class="col-sm-10 col-md-3">
<span class="add-new-icon glyphicon glyphicon-plus-sign" id="add_mobile"> </span>
@Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control",@id= "add_mobile" } })
@Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group row new_mobile_wrapper">
<div class="col-md-offset-3">
<div class="new_mobile_container">
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-offset-2 col-sm-10 col-md-2">
<input type="submit" value="Register" class="btn btn-primary col-sm-offset-1" />
</div>
</div>
}
@model PowerSupply.Models.CompanyAccountViewModel
@{
ViewBag.Title=“寄存器”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
注册
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“col-sm-2 col-md-1 col form label”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.Interval,htmlAttributes:new{@class=“col-sm-2 col-md-1 col form label”})
@EditorFor(model=>model.Interval,new{htmlAttributes=new{@class=“form control”})
时辰
@Html.ValidationMessageFor(model=>model.Interval,“,new{@class=“text danger”})
@LabelFor(model=>model.MobileNo,htmlAttributes:new{@class=“col-sm-2 col-md-1 col form label”})
@EditorFor(model=>model.MobileNo,new{htmlAttributes=new{@class=“form control”,@id=“add_mobile”})
@Html.ValidationMessageFor(model=>model.MobileNo,“,new{@class=“text danger”})
}
此处,所有字段均正确渲染,但:
@Html.EditorFor(model=>model.MobileNo,new{htmlAttributes=new{@class=“form control”,@id=“add_mobile”})
这不会生成任何标记。我指出了原因,并指出这是由于实体类型。MobileNo的实体类型为列表。如何修改此
@Html.EditorFor
?有什么想法吗?这可能是因为当CompanyAccountViewModel返回到视图时,您的MobileNo列表没有数据值。这可能是因为当CompanyAccountViewModel返回到视图时,您的MobileNo列表没有数据值。问题在于此属性:
public List<string> MobileNo { get; set; }
或者使用TextBoxFor
作为替代,也可以在循环中使用:
@for (var i = 0; i < Model.MobileNo.Count; i++)
{
@Html.TextBoxFor(m => m.MobileNo[i], new { @class = "col-sm-2 col-md-1 col-form-label" })
}
(变量i=0;i如果要从
列表
属性动态创建手机号码输入,请参阅开始。问题在于此属性:
public List<string> MobileNo { get; set; }
或者使用TextBoxFor
作为替代,也可以在循环中使用:
@for (var i = 0; i < Model.MobileNo.Count; i++)
{
@Html.TextBoxFor(m => m.MobileNo[i], new { @class = "col-sm-2 col-md-1 col-form-label" })
}
(变量i=0;i如果您想从
列表
属性动态创建手机号码输入,请参阅开始。是否要查找@Html.DropDownListFor?否,我想添加一个文本字段,然后使用jQuery添加其他文本字段,像单个字段一样添加验证。我以前就是这样做的,但我的列表是对象的集合,而不是字符串。也许这会让你走上正确的道路@Html.EditorFor(model=>model.Rate.Rate,new{htmlAttributes=new{@class=“form control”,Placeholder=“Rate”}})
,model.Rate是速率的集合。是否计划将值返回控制器?如果不是,则simple foreach循环将完成此任务。您无法将输入绑定到集合。您需要为集合中的每个值生成输入(在for
循环中)。是否查找@Html.DropDownListFor?否,我想添加一个文本字段,然后使用jQuery添加其他文本字段,像单个字段一样添加验证。我以前就是这样做的,但我的列表是对象的集合,而不是字符串。也许这会让你走上正确的道路@Html.EditorFor(model=>model.Rate.Rate,new{htmlAttributes=new{@class=“form control”,Placeholder=“Rate”}})
,model.Rate是速率的集合。是否计划将值返回控制器?如果不是,则simple foreach循环将完成此任务。您无法将输入绑定到集合。您需要为集合中的每个值生成一个输入(在for
循环中)。否!这是因为这是一个收藏品不!这是因为该属性是一个集合