Reputation: 2908
I'm loading a partial view in my C#/MVC4 application with the intents of updating the model with the basics of a shopping cart.
The partial view updates, but when I select the next item and submit, the viewmodel doesn't contain the data that was pushed the first time.
I have a class to store the data:
public class Selection
{
public class AllItems _AllItems { get; set; }
public int nCounter { get; set; }
public class Resource _Resource { get; set; }
}
My main view has this ajax form to post the data:
@using (Ajax.BeginForm("Index_AddItem", new AjaxOptions { UpdateTargetId = "AddItemList" }))
Then this is the class I'm working with in the partial view:
public class AllItems
{
public IEnumerable<SelectListItem> NewItemList { get; set; }
public int ID { get; set; }
public string ItemName { get; set; }
public DateTime dtAddUntil { get; set; }
[Display(Name = "Add Item")]
public List<MyApp.ViewModels.Item> AddItemList = new List<MyApp.ViewModels.Item>();
}
Everything displays ok on the main view and partial view.
(_AddItems.cshtml)
@model IEnumerable<MyApp.ViewModels.Item>
@{
ViewBag.Title = "Added Items";
}
<h2>Items to be Added</h2>
<table>
<tr>
<th>Item Name</th>
<th>Added until</th>
</tr>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>Html.DisplayFor(model=>item.ItemName)</td>
<td>Html.DisplayFor(model=>item.EndDate)</td>
</tr>
}
}
</table>
But when I add an item using:
[HttpPost]
public ActionResult Index_AddItem(Resource viewModel, string Text, string Value)
{
Value = Value.Trim();
List<Item> _items = new List<Item>();
string szItemName = GetItem(Convert.ToInt32(Value));
string test = Request.Form.GetValues("cbxPerm")[0].ToString();
Item NewItem = new Item();
NewItem.ItemName = szItemName;
_items.Add(NewItem);
return PartialView("_AddItems", _items);
}
The model sent to the function is empty.
The first item adds great and I see it in the partial view.
But subsequent adds remove the previously added item.
When I place a breakpoint in the AddItem function and look at the model; there's nothing in it.
Why isn't the model getting re-posted?
Upvotes: 0
Views: 180
Reputation: 20091
Let's understand what's happening in your code:
List<Item> _items = new List<Item>();
=> declare new list of type Item
.
string test = Request.Form.GetValues("cbxPerm")[0].ToString();
=>you are getting single string value & store into local string test.
Item NewItem = new Item();
=> declared new object NewItem
of type Item
.
string szItemName = GetItem(Convert.ToInt32(Value));
NewItem.ItemName = szItemName;
=> assign value converted to int from parameter value
to NewItem.ItemName
_items.Add(NewItem);
return PartialView("_AddItems", _items);
=>Add object NewItem
to _items
Problems
List<Item> _items = new List<Item>();
declares new list every time & you are adding only one value in it, so old values are removed.
You doesn't use new variable test
& parameters viewModel
& Text
anywhere.
Upvotes: 0
Reputation: 883
You can't really do it like this. You are creating a new list everytime:
List<Item> _items = new List<Item>();
You need to save this list somewhere and then fetch it and add items to it when you are using the additems function every time.
Either that or you save items client side and the post all of the at the same time to somewhere where they are saved.
Upvotes: 1
Reputation: 2530
The models for ASP.Net are server side, if you need client side models that can be updated etc you can make use of Knockout.js or other client side MVVM framework
Upvotes: 1