Why isn't my viewmodel updating?

别说谁变了你拦得住时间么 提交于 2019-12-11 20:59:58

问题


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?


回答1:


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




回答2:


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.




回答3:


Let's understand what's happening in your code:

  • List<Item> _items = new List<Item>();

    => declare new list of type Item.

    • In 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.



来源:https://stackoverflow.com/questions/23363813/why-isnt-my-viewmodel-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!