使用asp.net mvc从表选择中填充表单

时间:2014-03-26 20:49:10

标签: c# asp.net-mvc

我有一个包含数据的表格,当单击编辑按钮时,如何在同一页面上填充数据。基本上应该与此示例相同但不使用knockoutjs

http://jsfiddle.net/jiggle/2cr2f/

@model IEnumerable<GenomindApp2.Areas.RulesEngine.ViewModels.GeneViewModel>

@{
ViewBag.Title = "Index2";
}

<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.GeneValue)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.GeneCode)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.GeneName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.GeneComments)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.WildType)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.WildTypeAllele)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.AtRiskAllele)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>

    <td>
        @Html.DisplayFor(modelItem => item.GeneCode)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.GeneName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.GeneComments)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.WildType)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.WildTypeAllele)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AtRiskAllele)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new {  }) |
        @Html.ActionLink("Details", "Details", new {  }) |
        @Html.ActionLink("Delete", "Delete", new { })
    </td>
</tr>
}

</table>

1 个答案:

答案 0 :(得分:0)

你应该这样做:

型号:

public class ModelB
{
    public int Age { get; set; }
    public string Name { get; set; }
}

控制器:

public ActionResult MyAction()
{
    var model = new List<ModelB>
    {
        new ModelB{Age = 2, Name = "Bob"},
        new ModelB{Age = 7, Name = "Sam"},
    };
    return View(model);
}

[HttpPost]
public ActionResult MyAction(List<ModelB> model)
{
    //whatever
}

查看:

@model List<TestWebApplication.Models.ModelB>

...

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Count; i++)
    {
        Age: @Html.EditorFor(modelItem => Model[i].Age);
        Name: @Html.EditorFor(modelItem => Model[i].Name);
        <br />
    }

    <input type="submit"/>
}

请注意,我使用代替 代替 foreach 。填写表单时,不应该使用foreach - 它不能很好地呈现。