weixin_33737134 2013-02-15 16:13 采纳率: 0%
浏览 37

ASP.NET MVC3 Razor中的Ajax

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条回答

  • weixin_33739646 2013-02-15 16:26
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!