ASP.NET MVC3 Razor中的Ajax


                    

我对Ajax有问题。
我想制作多个表格,并在提交部分视图后替换包含一个表格的div。 当我只使用一种表格时,它对我有用。 我不知道我在做什么错。 也许是因为自定义编辑器的缘故?
这是我的代码,请尝试帮助我,我已经尝试了2天。</ p>

索引</ strong> </ p>

  @model MetriceWeb.Models.TaskInputModel
@ {
     ViewBag.Title =“任务”;
}


     任务</ h3>

@if(Model.Values.Count == 0)
{
   

目前没有任何待处理表格</ p>
}
其他
{
     @ Html.EditorFor(x => x.Values)
}
</ code> </ pre>

TaskInputValue(编辑器)</ strong> </ p>

  @model MetriceWeb.Models.TaskInputValue
@ {string s =“ task” + Model.TaskId;}

@using(Ajax.BeginForm(“ GetFromLibrary”,“ Metrice”,新的AjaxOptions
{
HttpMethod =“获取”,
UpdateTargetId = s,
InsertionMode =插入模式。替换
}))
{


@ Html.HiddenFor(x => x.TaskId,新的{@class =“ TaskId”,id =“ TaskId”})
@ Html.HiddenFor(x => x.InputId,新的{@class =“ InputId”})


@ Html.DisplayFor(x => x.Task)
</ h2>
已创建:</ span>
@ Html.DisplayFor(x => x.Date)


输入值

     @ Html.EditorFor(x => x.DateValue)
     @ Html.ValidationMessageFor(x => x.DateValue)
</ div>

</ div>


}
</ code> </ pre>

它是我的控制器中的输入方法,在其中我返回部分视图。 提交后,我的div不会被替换,整个新站点将被加载。 怎么了 请帮助我,我非常需要。</ p>
     </ div>

展开原文

原文

I have a problem with Ajax. I want to make multiple forms and replace div containing one after submiting with some Partial View. It's working for me when I use only one form. I don't know what I'm doing wrong here. Maybe it's becasue of custom editorfor? Here is my code, plese try to help me, I've been trying do this for 2 days.

Index

@model MetriceWeb.Models.TaskInputModel
@{
    ViewBag.Title = "Tasks";
}
<h3>
    Tasks</h3>

@if (Model.Values.Count == 0)
{
   <p>No pendings forms for you at the moment</p>
}
else
{
    @Html.EditorFor(x => x.Values)
}

TaskInputValue (editorfor)

@model MetriceWeb.Models.TaskInputValue
@{string s = "task" + Model.TaskId;}

@using (Ajax.BeginForm("GetFromLibrary", "Metrice", new AjaxOptions
{
HttpMethod = "Get",
UpdateTargetId = s,
InsertionMode = InsertionMode.Replace
}))
{
<div class="editor-field" id="@s">
@Html.HiddenFor(x => x.TaskId, new { @class = "TaskId" , id = "TaskId" })
@Html.HiddenFor(x => x.InputId, new { @class = "InputId" })
<h2>
@Html.DisplayFor(x => x.Task)
</h2>
<span>Created: </span>
@Html.DisplayFor(x => x.Date)

<div>
Input Value

    @Html.EditorFor(x => x.DateValue)
    @Html.ValidationMessageFor(x => x.DateValue)
</div>
<input type="submit" value="Submit" />
</div>
<br />
}

It's entering method in my controller where I'm returning partial view. After submitting my div isn't replacing, the whole new site is loaded. What is wrong here? Please help me, I am in big need.

1个回答


为了使像 Ajax.BeginForm </ code>这样的 Ajax </ code>助手正常工作(例如发送AJAX请求),您需要在视图中引用以下JavaScript文件</ p>

  jquery.unobtrusive-ajax.js
</ code> </ pre>

(当然还有那个jquery)</ p>

来自 ASP.NET MVC 3中不引人注目的Ajax </ a> </ p>


  

一个有趣的注释:由于没有实际的JavaScript发出
   当您使用通俗易懂的Ajax时,如果忘记包含一个或
   其他脚本,尝试提交该脚本时不会看到任何错误
   Ajax请求; 它只会表现得像非Ajax请求一样。</ p>
</ blockquote>
     </ div>

展开原文

原文

In order to the Ajax helpers like Ajax.BeginForm work correctly (e.g sending an AJAX request) you need reference in your view the following JavaScript file

jquery.unobtrusive-ajax.js

(and of course before that jquery)

From the Unobtrusive Ajax in ASP.NET MVC 3

An interesting note: since there is no actual JavaScript being emitted when you use unobtrusive Ajax, if you forget to include one or the other script, you won’t see any errors when attempting to submit the Ajax request; it will simply behave like a non-Ajax request.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐