A quick question regarding updating the list of items in asp.net mvc.
Basically, I have an editing action method that returns a collection of objects (by the way, the structure of the table looks like this: "testID, assetID, Result" is the link table).
Basically, I want these elements to appear one after another in the form and be able to edit them. The form should be sent back, and the blender model does its magic. But, it is not so simple.
I looked through the web, and it seems that most of the information about this material seems to be a bit outdated. I met this post which has not been updated for a long time, and this one which seems to suggest that you should not be tied to an existing list for updating and that there are problems when working with EF or Linq to Sql (which I am).
Is there an easy way to achieve what I want? Is the list model binding state changed in the release version?
UPDATE - a little closer ...
Here is my editing method:
public ActionResult EditSurveyResults(Guid id)
{
var results = surveyRepository.GetSurveyResults(id);
return PartialView("EditSurveyResults", results);
}
And my form:
<div id="editSurveyResults">
<h2>
EditSurveryResults</h2>
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm())
{%>
<fieldset>
<legend>Results</legend>
<% int i = 0; foreach (var result in Model)
{ %>
<input type="hidden" name='results[<%= i %>].TestID' value='<%= result.TestID %>' />
<input type="hidden" name='results[<%= i %>].AssetID' value='<%= result.AssetID %>' />
<p>
<%= result.Task.TaskName%>
</p>
<p>
<label for="Result">
Result:</label>
<input type="text" name='results[<%= i %>].Result' value='<%= result.Result %>' />
<%= Html.ValidationMessage("Result", "*")%>
</p>
<% i++; } %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
And my post editing method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditSurveyResults(Guid id, IList<SurveyTestResult> results)
{
var oldValues = surveyRepository.GetSurveyResults(id);
if (ModelState.IsValid)
{
UpdateModel(oldValues);
surveyRepository.Save();
return Content("Done");
}
else
return PartialView("EditSurveyResults");
}
This is not complete, of course, but it does not update anything in its current state. Did I miss the trick here? the results are populated with updated objects, so I'm not sure why it is not updating ...
2:
, , . , , . - , , . FYI - AJAX, , .
:
IList<SurveyTestResult> oldValues = surveyRepository.GetSurveyResults(id).ToList();
foreach (var result in SurveyTestResult)
{
SurveyTestResult thisone = oldValues.Single(p => p.AssetID == result.AssetID &&
p.TestID == result.TestID);
thisone.Result = result.Result;
}
"" .