开发者

update data using foreach loop MVC

开发者 https://www.devze.com 2023-02-12 03:47 出处:网络
I have a update stored proc that updates the DisplayTestimonials field in the database based on what the users checks on the form. How would I update the checked field in the database in my controller

I have a update stored proc that updates the DisplayTestimonials field in the database based on what the users checks on the form. How would I update the checked field in the database in my controller? I guess i would have to use controller to call that update stored procedure, right? I am not sure how would i pass the values from a foreach loop in a controller. Any help?

This is my View.

@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>
            &开发者_高级运维lt;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>
}


To bind collections to your model and pass it to controller you need index values in html-form. Read this blogposts for example: Phil Haack - Model Binding To A List and nmarun - ASP.NET MVC 2 Model Binding for a Collection.


You did not show the Html.BeginForm("MyMethod", "MyController") view code so I am going to use generic names.

You would need a controller action method as such:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号