Asp.net mvc MVC中的可编辑表

Asp.net mvc MVC中的可编辑表,asp.net-mvc,Asp.net Mvc,我一直不知道如何编辑网页上的表项,并将它们发布回服务器。实际上,在本例中,我只希望用户选择复选框,就像他们选择了多个项目一样,但这只是实现 问题是,当视图显示在网页上时,在HttpPost中,picSelections为空 这是我的观点: @model IEnumerable<CarShow.lmit_pics> @using (Html.BeginForm()) { <div style="width:100%; height:60px; "> <inp

我一直不知道如何编辑网页上的表项,并将它们发布回服务器。实际上,在本例中,我只希望用户选择复选框,就像他们选择了多个项目一样,但这只是实现

问题是,当视图显示在网页上时,在HttpPost中,picSelections为空

这是我的观点:

@model IEnumerable<CarShow.lmit_pics>

@using (Html.BeginForm())
{    
<div style="width:100%; height:60px; ">
<input type="submit" value="Add Selected Photo(s) to Survey" />
</div>

    for (int i = 0; i < Model.Count(); i++)
    {
        var item = Model.ElementAt(i);
    <div style="width:296px; float:left">

            <div style="float:left; width:200px; margin-bottom:18px">

                @{string cropped = "../../../Content/Images/" + Html.DisplayFor(modelItem => item.picname).ToString() + "?" + Html.DisplayFor(modelItem => item.version).ToString();
                 }
                <img alt="picture" class="pic-size" src="@cropped" />
                <br />
                    @Html.EditorFor(modelItem => item.brand)
            </div>

</div>
        }    
}
</div>
这在控制器中:

public ActionResult AddSurveyPic(string id)
{
   var validPicSummaries = (IEnumerable)(from x in db.lmit_pics where x.enabled == 1 select x);
   return View((IEnumerable)validPicSummaries);
}

[HttpPost]
public ActionResult AddSurveyPic(IEnumerable<CarShow_MVC.lmit_pics> picSelections)
{
        // Save data here
}

谢谢。

不要在视图中写循环。我建议您使用编辑器模板:

@model IEnumerable<CarShow.lmit_pics>

@using (Html.BeginForm())
{    
    <div style="width:100%; height:60px; ">
        <input type="submit" value="Add Selected Photo(s) to Survey" />
    </div>
    @Html.EditorForModel()
}

您可能还需要阅读,以便更好地理解默认模型绑定器在绑定集合和词典时使用的命名约定。

可编辑表是否位于POST中?我想是这样。我只是添加了更多的代码。我还没有添加可编辑的字段,但我认为这不会影响帖子。也许我做错了?如果没有可编辑的字段,就不会有任何用于发布到控制器actionJason的编码,我在我的一个字段上添加了一个HTML.EditorFor,得到了相同的结果。我更新了上面的视图代码以反映更改。不要在视图中编写循环-真的吗?这是MVC1的面包和黄油,或者至少是黄油。事情有这么大的变化吗?是的,随着ASP.NET MVC 2中强类型助手和编辑器/显示模板的引入,事情发生了巨大的变化。从现在起,您就不再需要在视图中编写循环了。我对MVC非常陌生,编辑器模板对我来说是全新的。现在加载时间似乎快多了。MVC太棒了!不过,有一件事是,HttpPost现在仍然没有捕捉到结果。我会对此进行研究,但你知道我还可能做错了什么吗?你的意思是picSelections在后期操作中仍然为空?它可以工作。。。很抱歉,我更改了HttpPost,然后又更改了回去,现在一切都好了。您的代码工作正常,请先尝试。非常感谢!
@model CarShow.lmit_pics

<div style="width:296px; float:left">
    <div style="float:left; width:200px; margin-bottom:18px">
        @{string cropped = "../../../Content/Images/" + Html.DisplayFor(x => x.picname).ToString() + "?" + Html.DisplayFor(x => x.version).ToString(); }
        <img alt="picture" class="pic-size" src="@cropped" />
        <br />
        @Html.EditorFor(x => x.brand)
    </div>
</div>