c# - MVC3 - Multiple Ajax forms in a partial view are all filled with the same data on refresh -


i'm learning mvc3 , building little "to-do" website learning exercise, i'm open idea i'm going down wrong path!

anyway, have page working regular postbacks. i'm trying ajax jquery , unobtrusiveajax , still technically works correctly (the data passed controller , saved in database). problem in element replace, each form's fields filled values passed in on 1 form.

index.cshtml

@model webui.models.homeviewmodel  @{     viewbag.title = "index";     <script src="@url.content("~/scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>     <script src="@url.content("~/scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> }  <h2>my goals</h2> ...snip... <div id="goal_div"> @foreach (var goal in model.goals) {     html.renderpartial("goaldetail", goal); } </div> 

goaldetail.cshtml

@model domain.entities.goal <div class="goal" id='goal_id@(model.id)'>     <h4>@model.name</h4>     <p>@datetime.now.tostring()</p>     <p class="goal_description">@model.progress % complete</p>     <ul>         @foreach (var task in model.tasks)         {             using (ajax.beginform("updatetask", "home", new ajaxoptions { updatetargetid = "goal_id" + model.id }))             {                 @html.hiddenfor(g => g.id)                 @html.hidden("taskid", task.id)                 <li class="task">                     @html.checkbox("iscomplete", task.iscomplete)                      @html.textbox("taskname", task.name)                     @task.name                     <input class="actionbuttons" type="submit" value="update task" />                 </li>             }         }         <li>         @using (html.beginform("addtask", "home"))         {             @html.hiddenfor(g => g.id)             @html.editor("taskname")             <input class="actionbuttons" type="submit" value="add task" />         }         </li>     </ul> </div> 

homecontroller.cs

public class homecontroller : controller {     private igoalrepository repository;      public homecontroller(igoalrepository repo)     {         repository = repo;     }      public viewresult index()     {         homeviewmodel viewmodel = new homeviewmodel();         viewmodel.goals = repository.goals;         return view(viewmodel);     }      public actionresult addtask(int id, string taskname)     {         bool success = repository.savetask(id, 0, taskname, datetime.today, false);         return redirecttoaction("index");     }      public actionresult updatetask(int id, int taskid, bool iscomplete, string taskname)     {         bool success = repository.savetask(id, taskid, taskname, datetime.today, iscomplete);         goal updatedgoal = repository.goals.firstordefault(g => g.id == id);         return partialview("goaldetail", updatedgoal);     }      public actionresult addgoal(string name, datetime enddate)     {         bool success = repository.savegoal(0, name, datetime.today, enddate);         return redirecttoaction("index");     }      public actionresult updategoal(int goalid, string name, datetime enddate)     {         bool success = repository.savegoal(goalid, name, datetime.today, enddate);         return redirecttoaction("index");     } } 

i have time there make sure ajax refresh has happened, , you'll see why have task name there twice.

this see when first load page: before

then check checkbox of the 2nd task of 1st goal, rename "updated task #2", , click update button. that's when happens: after

seeing how task names not part of form correct (ignoring re-ordering now), , progress value has been updated correctly (it takes completed tasks , divides total number of tasks), have no idea why form values have been replaced. addtask form has been filled in, though haven't changed 1 use ajax yet. i've been searching reasons 2 days now, , have come empty.

after more searching, discovered issue. basically, has way modelstate works in mvc. reading this thread , this article helped me understand happening page. ended calling modelstate.clear() in controller right before returning partial view, this question , answer suggests method.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -