1

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>
            <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>
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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
       }
    }
}

2 Comments

I get an error on the foreach loop.System.NullReferenceException: Object reference not set to an instance of an object.
Also, i have updated my post to include the full code for View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.