简体   繁体   English

用另一个替换部分视图

[英]Replace Partial View with another

I'm facing some issue while trying to replace a partial view with another, I did my research and tried to achieve my goal with some topics about that kind of issues. 我在尝试用另一种视图替换部分视图时遇到了一个问题,我进行了研究,并尝试通过一些有关此类问题的主题来实现自己的目标。 I'm quite unhandy with asp.net so far, but i understood that i need to use Ajax.ActionLink 到目前为止,我对asp.net不太方便,但我知道我需要使用Ajax.ActionLink

What i've done so far is this. 我到目前为止所做的就是这个。

I have a list of items that a display thanks to a partial view, now what i'd like to do is this, when i click on one partial view link "edit" i want this specific view to be replaced with another partial view aimed to edit some fields, then i want to click to save and go back to the other partial view "details" pretty simple isn't it? 我有一个要归因于局部视图的项目列表,现在我要做的是,当我单击一个局部视图链接“编辑”时,我希望将此特定视图替换为另一个局部视图编辑一些字段,然后我要单击保存并返回到其他局部视图“详细信息”,这很简单,不是吗? but i can't get through this so far. 但到目前为止我还无法解决。

Here's some code, here i just put the code relative to the replacement of _ItemSummary partial view with _ItemEdit partial view 这是一些代码,在这里我只是将代码与_ItemSummary部分视图替换为_ItemEdit部分视图相关

MyController.cs MyController.cs

public ActionResult Index()
{
var list = (some code to get my list of items)
return View(list);
}

public ActionResult ItemEdit(string itemId)
{
var item = (some code to get my item thanks to its id)
return PartialView("_ItemEdit",item);
}

Index.cshtml Index.cshtml

@model IEnumerable<ItemModel>
<div class="row main-elem main-with-menu">
    <div class="box">
        @foreach (var item in Model)
        {
            @Html.Partial("_ItemSummary",item)
        }
    </div>
</div>

_ItemSummary.cshtml _ItemSummary.cshtml

<div id="itemsummary" class="thumbnail">
    <div class="col-xs-12">
        <div class="caption">
            <div class="col-xs-12" id="infosObj">
                <p id="room-name" class="room-name">
                    <span class="roomName">@Model.Id</span>
                </p>
                <h5>@Model.Reference</h5>
                <h5>@Model.Localisation</h5>

                @Ajax.ActionLink( "Edit", "ItemEdit", "Settings", new { itemId = Model.Id }, new AjaxOptions() { UpdateTargetId = "itemsummary", HttpMethod = "GET", InsertionMode = InsertionMode.Replace}  )

            </div>
        </div>
    </div>
</div>

_ItemEdit.cshtml _ItemEdit.cshtml

<div id="itemedit" class="thumbnail">
    <div class="col-xs-12">
        <div class="caption">
            <div class="col-xs-12" id="infosObj">
//Stuff to be added
            </div>
        </div>
    </div>
</div>

What it currently does is to redirect me to a page http://domain.eg/Settings/ItemEdit?itemId=item_1 but i want to stay on my page http://domain.eg/Settings and just replace the div i clicked on, but in the controller the ItemEdit function is called with the correct itemId. 当前的作用是将我重定向到页面http://domain.eg/Settings/ItemEdit?itemId=item_1,但我想保留在页面http://domain.eg/Settings上 ,只替换我单击的div打开,但在控制器中使用正确的itemId调用ItemEdit函数。 i read about that kind of issues and tried some of the answers but i can't achieve my purpose. 我阅读了此类问题并尝试了一些答案,但我无法实现我的目的。

Thanks in advance for some explainations of what i'm doing wrong and what should be done instead 预先感谢您对我做错了什么以及应该怎么做的一些解释

You can do it yourself, by adding some script : 您可以自己添加一些脚本:

<div id="itemsummary" class="thumbnail">
    <div class="col-xs-12">
        <div class="caption">
            <div class="col-xs-12" id="infosObj">
                <p id="room-name" class="room-name">
                    <span class="roomName">@Model.Id</span>
                </p>
                <h5>@Model.Reference</h5>
                <h5>@Model.Localisation</h5>
                <a class="editLink" href="@Url.Action("ItemEdit", "Settings", new { itemId = Model.Id  })" >Edit </a>                  
            </div>
        </div>
    </div>
</div>

$(".editLink").on("click", function(){
    event.preventDefault();
    var link = $(this);
    var href= link.attr("href");

    $.ajax({ url: href })
    .done(function( data ) {
        link.hide().after(data);
    });   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM