为什么我的视图模型更新? [英] Why isn't my viewmodel updating?

查看:111
本文介绍了为什么我的视图模型更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载的局部视图与更新与购物车的基本模型的意图我的C#/ MVC4应用。

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; }
}

我的主视图中有这样的Ajax表单张贴数据:

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);
}

发送给函数的模型是空的。结果
第一项增加了伟大的,我看到它的局部视图。结果
但随后的增加删除previously添加的项目。结果
当我把一个断点在函数的AddItem和看模型;没有什么在里面。结果
为什么不是模型越来越重贴?

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?

推荐答案

有关ASP.Net的模型是服务器端,如果需要可更新等,您可以利用Knockout.js或其他客户端的客户端模式MVVM框架

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

这篇关于为什么我的视图模型更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆