Asp.net mvc 3 使用foreach循环MVC更新数据

Asp.net mvc 3 使用foreach循环MVC更新数据,asp.net-mvc-3,Asp.net Mvc 3,我有一个更新存储过程,它根据用户在表单上检查的内容来更新数据库中的displayertentials字段。如何更新控制器中数据库中的选中字段?我想我必须使用控制器来调用那个更新存储过程,对吗?我不确定如何从控制器中的foreach循环传递值。有什么帮助吗 这是我的看法 @model Models.SurveyTestimonials @{ Layout = "~/CustomViews/cap/Shared/_DealerLayout.cshtml"; } @section Tags

我有一个更新存储过程,它根据用户在表单上检查的内容来更新数据库中的displayertentials字段。如何更新控制器中数据库中的选中字段?我想我必须使用控制器来调用那个更新存储过程,对吗?我不确定如何从控制器中的foreach循环传递值。有什么帮助吗

这是我的看法

@model Models.SurveyTestimonials

@{
    Layout = "~/CustomViews/cap/Shared/_DealerLayout.cshtml";
}
@section Tags {

        @Html.UserControl("Header", new { id = Model.Channel.ChannelId })

}

@section Menu {
    @Html.ActionLink("Premier Dealer", "Index", "Premier", new { area = "Apps" }, new { })
}      
<h2>Manage Survey Testimonials</h2>
@using (Html.BeginForm())
{
<div>
<table>
    <thead>
        <tr>
            <td>Select</td>
            <td>First Name</td>
            <td>Last Name</td>
            <td>Testimonial</td>
        </tr>
    </thead>
@foreach (var testimonials in Model.Testimonials)
{
    <tr>
        <td>@Html.CheckBox("" + testimonials.DisplayTestimonials)
            @Html.Hidden(testimonials.ResponseId.ToString())
        </td>
        <td>@Html.Label(testimonials.FirstName)</td>
        <td>@Html.Label(testimonials.LastName)</td>
        <td>@Html.Label(testimonials.Question5Answer)</td>
    </tr>   
}
<tr>
    <td colspan="3">
        <input type="submit" id="Submit" value="Save" class="PremireButton" />
    </td>
</tr>
</table>
</div>
}
@model Models.SurveyTestimonials
@{
Layout=“~/CustomViews/cap/Shared/_dealrelayout.cshtml”;
}
@节标记{
@UserControl(“Header”,新的{id=Model.Channel.ChannelId})
}
@区段菜单{
@ActionLink(“卓越理财经销商”、“索引”、“卓越理财”、新{area=“Apps”}、新{})
}      
管理调查证明
@使用(Html.BeginForm())
{
挑选
名字
姓
证明信
@foreach(模型中的var证明文件。证明文件)
{
@Html.CheckBox(“+证明文件.显示证明文件”)
@Html.Hidden(推荐的.ResponseId.ToString())
@Html.Label(推荐的名字)
@Html.Label(证明文件.姓氏)
@Html.Label(推荐性问题5答案)
}
}

您没有显示
Html.BeginForm(“MyMethod”、“MyController”)
查看代码,因此我将使用泛型名称

您需要一个控制器操作方法,如下所示:

public ActionResult MyMethod(MyModel model)
{
    foreach (var t in model.Testimonials)
    {
       if (t.DisplayTestimonials)
       {
           // do update logic
       }
    }
}

您没有显示
Html.BeginForm(“MyMethod”、“MyController”)
查看代码,因此我将使用泛型名称

您需要一个控制器操作方法,如下所示:

public ActionResult MyMethod(MyModel model)
{
    foreach (var t in model.Testimonials)
    {
       if (t.DisplayTestimonials)
       {
           // do update logic
       }
    }
}

要将集合绑定到您的模型并将其传递给控制器,您需要html格式的索引值。阅读本博客文章,例如:和。

要将集合绑定到模型并将其传递给控制器,您需要html格式的索引值。阅读此博客帖子,例如:and.

我在foreach loop.System.NullReferenceException:对象引用未设置为对象的实例时出错。此外,我已更新帖子以包含查看的完整代码。我在foreach loop.System.NullReferenceException:对象引用未设置为对象的实例时出错。此外,我已经更新了我的帖子以包含完整的代码供查看。