我如何刷新@ html.Action从控制器 [英] How do I refresh @html.Action from a controller

查看:85
本文介绍了我如何刷新@ html.Action从控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,在左手边是资产的菜单列表,当你点击一个资产然后在右侧会出现一个表单编辑该资产。我需要能够刷新资产的编辑后资产的列表,以便在名称中的任何更改菜单列表上出现为好。

I have a page that on the left hand side is a menu list of assets, that when you click on an asset then a form appears on the right hand side to edit that asset. I need to be able to refresh the list of assets after an edit of an asset so that any changes in the name appear on the menu list as well.

下面是我页

 </head>
    <body>
        <div id="leftHandMenu">
        <h2>assets</h2>
        @Ajax.ActionLink("Create Asset", "CreateAsset", new AjaxOptions() { UpdateTargetId = "FormContainer", InsertionMode = InsertionMode.Replace })
        @Html.Action("Assets")
        </div>
        <div id="FormContainer">
        </div>
    </body>
</html>

资产的(资产)调用使用@ html.Action创建的列表。
下面是资产的列表

The List of assets in created using the @html.Action("Assets") call. Here is the view for the list of assets

 @model IList<CasWeb.Models.DataContext.Asset>
 @if (Model != null && Model.Any())
 {
     <ul>
         @foreach (var asset in Model)
         {

             <li> @Ajax.ActionLink(asset.Name, "EditAsset", new { id = asset.Id }, new AjaxOptions() { UpdateTargetId = "FormContainer", InsertionMode = InsertionMode.Replace }) </li>
         }
     </ul> 
    }else
         {
             <h1> no assets</h1>
         }

这是我的编辑资产视图

And here is my Edit Asset View

 @using System.Collections
@model CasWeb.Models.DataContext.Asset


<script type="text/javascript">

    $(function () {

        $('form').submit(function () {
            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        $('#FormContainer').html(result);
                    }
                });
            }
            return false;
        });
    });
</script>


@using (Html.BeginForm())
    {
        @Html.ValidationSummary()

        <fieldset>

            <legend>Asset</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.ModelId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.ModelId, new SelectList((IEnumerable)ViewData["AssetModels"], "Id", "Model"))
                @Html.ValidationMessageFor(model => model.ModelId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.SizeId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.SizeId, new SelectList((IEnumerable)ViewData["AssetSizes"], "Id", "Size"))
                @Html.ValidationMessageFor(model => model.SizeId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.TypeId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.TypeId, new SelectList((IEnumerable)ViewData["AssetTypes"], "Id", "Type"))
                @Html.ValidationMessageFor(model => model.TypeId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.DeptId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.DeptId, new SelectList((IEnumerable)ViewData["Depts"], "Id", "Name"))
                @Html.ValidationMessageFor(model => model.DeptId)
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

这是我的控制器

[ChildActionOnly]
public PartialViewResult Assets()
{
    var assets = _assetRepo.GetAll();
    return PartialView(assets);
}

public PartialViewResult EditAsset(Guid id)
{
    SetUpViewDataForComboBoxes();

    var asset = _assetRepo.Get(id);
    return asset == null
               ? PartialView("NotFound")
               : PartialView("EditAsset", asset);
}

[HttpPost]
public PartialViewResult EditAsset(Asset asset)
{

    if (ModelState.IsValid)
    {
        _assetRepo.Update(asset);
        return PartialView("EditAssetSuccess");
    }

    SetUpViewDataForComboBoxes();

    return PartialView("EditAsset", asset);
}

我希望能够刷新资产EditAsset POST方法查看控制器后保存,因为这是更容易测试,但我可以告诉我可能需要在成功呼吁要做到这一点回来编辑表单Java脚本提交。

I would like to be able to refresh the assets view in EditAsset post method in the controller after the save, as this is more testable, but from what I can tell I may need to do this in the success call back for the edit form java script submit.

推荐答案

有关编辑入资产,使用下面的方法的onSuccess事件的Ajax表单和呼叫。您还需要以下页面加载方法来调用。

For the Edit Assest, use Ajax form and call following method OnSuccess event. You also need to call following method on page load.

$.get("/ControllerName/Assets", { random: '@DateTime.Now.Ticks' }, function (response) {
                $("#ListOfAssestDiv").html(response);                
            });

有关显示入资产列表我的意思是放在另一个div(@ Html.Action(资产))。所以,你的code变成了这个样子

For display assest list i mean ( @Html.Action("Assets")) put in another div. So your code become like this

 </head>
    <body>
        <div id="leftHandMenu">
        <h2>assets</h2>
        @Ajax.ActionLink("Create Asset", "CreateAsset", new AjaxOptions() { UpdateTargetId = "FormContainer", InsertionMode = InsertionMode.Replace })
<div id="ListOfAssestDiv">
        @Html.Action("Assets")
</div>
        </div>
        <div id="FormContainer">
        </div>
    </body>
</html>

请让我知道如果您有任何疑问。

Please let me know if you have any query.

这篇关于我如何刷新@ html.Action从控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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