C# 提交 Ajax.Beginform 后更新局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14801322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:01:59  来源:igfitidea点击:

Update partial view after submit Ajax.Beginform

c#ajaxasp.net-mvcasp.net-mvc-4partial-views

提问by Andreas

I am new at .NET mvc.

我是 .NET mvc 的新手。

In a view "DisplayThings" I have something like:

在“DisplayThings”视图中,我有类似的东西:

@foreach (var thing in Model)
{
    @Html.Partial("DisplayPartial", thing)
}

In the partial view "DisplayPartial" I have

在局部视图“DisplayPartial”中,我有

@using (Ajax.BeginForm("Afunc", new AjaxOptions ()))
{
    @Html.EditorFor(model => model.AstringThing)
    @Html.EditorFor(model => model.AintThing)

    <input type="submit" name="submit" value="Ajax Post" />
}

At the moment the "Afunc"-Action saves the model to the database and then redirects to a controller action to retrieve all "things" from the database and render the entire "Displaythings" View.

目前,“Afunc”-Action 将模型保存到数据库,然后重定向到控制器操作以从数据库中检索所有“事物”并呈现整个“Displaythings”视图。

My question is: When i press one of the submitbuttons (There is one submitbutton for every "thing" i the list). I want only that partial view to reload/reflect on my change. I dont want to reload the entire "Displaythings" view. How do I do this? If I just return a partial view I lose everything else but that partial view.

我的问题是:当我按下其中一个提交按钮时(列表中的每个“事物”都有一个提交按钮)。我只希望该部分视图重新加载/反映我的更改。我不想重新加载整个“Displaythings”视图。我该怎么做呢?如果我只是返回一个局部视图,除了那个局部视图之外,我会失去其他一切。

If this is a bad approach please give me directions.

如果这是一个不好的方法,请给我指示。

Update:

更新:

I am still doing something wrong as I get the partial view rendered in a new page. My controller :

当我在新页面中呈现部分视图时,我仍然做错了什么。我的控制器:

public ActionResult Afunc(ThingModel thingmodel)
{
    // do

    return PartialView("DisplayPartial", thingmodel);
}

I have tried using UpdateTargetId and onsuccess both with the same result (A new page)

我尝试使用 UpdateTargetId 和 onsuccess 都得到相同的结果(新页面)

采纳答案by manojlds

In the AjaxOptionsthat your are simply now passing as new AjaxOptionsyou can specify the target element using the UpdateTargetIdproperty:

AjaxOptions你的只是现在正在为new AjaxOptions您可以使用指定目标元素UpdateTargetId属性:

<div id="unique_thing_id">
@using (Ajax.BeginForm("Afunc", new AjaxOptions { UpdateTargetId = 'unique_thing_id' }))
{    
}
</div>

Above, a container with unique id for every "thing" is represented with <div id="unique_thing_id">. This will be replaced with the repsonse of the request. Change Afuncto render only the particular "thing" partial.

上面,每个“事物”都具有唯一 id 的容器用<div id="unique_thing_id">. 这将替换为请求的响应。更改Afunc为仅呈现特定的“事物”部分。