0

I am passing a list from my controller to my view, and then inside a form I loop through each list item and display a table with some textboxes that need to be filled in for each item.

What I then want to do is submit the data back to my controller, look through it to grab the results of what was entered, and save to database.

How do I do this?? I have put my code below

CONTROLLER

public ActionResult GetListOfDogs()
{
var list = db.dogs;  //gets multiple dogs 
return View(dogs);

///Dog object looks like this
///int Id 
///string DogName 
///string RegNumber 
///string Breed 
}

[HttpPost]
public ActionResult SaveDogInfo(string[] RegNumber, string[] Breed)
{

}

VIEW

GetListOfDogs.cshtml

@model Dog

@using(Html.BeginForm("SaveDogInfo", "Dog", FormMethod.Post))
{
<table>
    <tr>
        <th>Name</th>
        <th>Reg Number</th>
        <th>Breed</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.DogName</td>
            <td>@Html.TextBox("RegNum", null, new{id = "dId_=" + @item.Id})</td>
            <td>@Html.TextBox("Breed", null, new{id = "breed_=" + @item.Id})</td>
        </tr>
    }
</table>
<input type="Submit" value="Save" />

}

Inside my 'SaveDogInfo' action, I can view the results for RegNumber and Breed, but there is no id coming back with them, so I have no way of matching the result with the actual DogId it belongs to..

Can anyone help or am I just totally missing the point???

Cheers Chris

1 Answer 1

1

Your model of your view should be something enumerable, no ?

@model IList<Dog>

then use a for loop, and add an hidden field for Id. !

@for (var i = 0; i < Model.Count(); i++) 
    {
        <tr>
            <td>@Html.DisplayFor(m => Model[i].Name</td>
            <td>@Html.TextBoxFor(m => Model[i].RegNumber)</td>
            <td>
                @Html.TextBoxFor(m => Model[i].Breed)
                @Html.HiddenFor(m => Model[i].Id)
           </td>
        </tr>
    }

then your post action should become

[HttpPost]
public ActionResult SaveDogInfo(IEnumerable<Dog> dogs)
{

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

5 Comments

yeah sorry it is @model IEnumerable<Dog>
I have modified my view to use the for loop, and looking at the source, I can see that the Id has been put into the hidden field. On my controller I have also changed it to expect IEnumerable<Dog>, but when I put a breakpoint to view what is inside dogs, I am getting dogs is null
Ok... so I have got it working now... <br />I had to make a couple of changes... <br />Using IEnumerable<Dog> I was getting an error when trying to index the row (@Html.TextBoxFor(m=>Model[i].)) it turns out you cannot apply indexing to type IEnumerable, so instead I tried to use @Html.TextBoxFor(m=>Model.ElementAt(i).property) and that allowed the text box to display correctly, however it was then that I was getting the null being returned to the controller... <br /> So I went back to the original idea of using Model[i], but chnaged my type to IList<> and now it is working correctly.
@jonahpup yes, I corrected in same way just now, sorry for that.
that's all good... I appreciate the help, at least it got me on the right track

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.