如何允许用户使用 @Html.EditorForModel 将项目添加到列表中?



>我有一个名为参数的类。

public class Parameters
{
public string Name { get; set; }
//other properties
public List<LatLong> Coordinates { get; set; }
}

拉特龙是这样的:

public class LatLong
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}

在前端,我有以下用于创建新的参数类的表单。

@model Parameters
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.EditorForModel()
}

我有一个列表的编辑器模板: Views/Shared/EditorTemplates/LatLongList.cshtml

@model List<LatLong>
@foreach (var ll in Model)
{
@Html.EditorForModel()
}

以及一个用于 LatLong 的编辑器模板: Views/Shared/EditorTemplates/LatLong.cshtml

@model LatLng
@Html.EditorFor(model => model.Latitude)
@Html.EditorFor(model => model.Longitude)

但是,什么也没出现。

如何允许用户输入任意数量的坐标?

这是一个工作示例,您可以参考

主视图:

@model MVC3_0.Models.ParameterModel.Parameters
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.EditorForModel("AddParameter")
}

EditorTemplate for AddParameter:Views/Shared/EditorTemplates/AddParameter.cshtml

@model MVC3_0.Models.ParameterModel.Parameters
<div>
<div>
<label> Name:</label>
@Html.TextBoxFor(x => x.Name)
</div>
<div>
<label>Count:</label>
@Html.TextBoxFor(x => x.Count)
</div>
<div>
<label>Coordinates:</label>
//Editor Template for LatLongList
@Html.EditorFor(x => x.Coordinates)
</div>
</div>

EditorTemplate for LatLong :Views/Shared/EditorTemplates/LatLong.cshtml ,EditorFor 将为您迭代集合

@model MVC3_0.Models.ParameterModel.LatLong
<div>
<label>Latitude:</label>
@Html.TextBoxFor(model => model.Latitude)
</div>
<div>
<label>Longitude:</label>
@Html.TextBoxFor(model => model.Longitude)
</div>

参考:

我应该如何使用其参数调用"EditorForModel"?

https://forums.asp.net/t/1948071.aspx?EditorFor+and+EditorForModel

最新更新