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
}
}
}
精彩评论