ورود

View Full Version : راهنمایی در مورد ajax



hamix666
جمعه 28 مهر 1396, 14:24 عصر
سلام من یک لیست دارم

@using System.Linq
@using System.Web
@model System.Collections.Generic.List<DomainModel.ViewModel.Gourop.GroupVm>


@helper ListGroup(int? parentId, string space)
{
foreach (var item in Model.Where(x => x.ParentId == parentId).ToList())
{
<option value="@item.Id">@space +@item.Name</option>
@Html.Raw(HttpUtility.HtmlDecode(ListGroup(item.Id , space + "&nbsp;&nbsp;&nbsp;&nbsp;").ToString()));


}
}




<div id="dvgouropList">
<select id="ParentId" name="ParentId" size="15" style="width: 250px">
<option value="">گروه اصلی</option>
@ListGroup(null, "")
</select>
</div>




و یک ویو دیگر دارم که این لیت در آن لود می شود


@model DomainModel.ViewModel.Gourop.GroupVm
@{
ViewBag.Title = "User";
Layout = "~/Areas/Panel/Views/Shared/_LayoutPanel.cshtml";
}
@using (Html.BeginForm("AddGroup", "HomePanel", FormMethod.Post,new {id= "groupForm" }))
{
<div>
<div class="form-group col-lg-6">
@Html.Label("نام دسته", htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"})
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" id="btnAddd" value="ثبت" class="btn btn-default" />
</div>
</div>
</div>




<div>
@Html.Partial("_GroupList",Model.Grops)
</div>


</div>
}
@section Scripts{
<script type="text/javascript">
$(function() {
$("#btnAddd").click(function (e) {
e.preventDefault();
if ($("#groupForm").valid()) {
$.ajax({
url: @Url.Action("AddGroup","HomePanel"),
data: $("#groupForm").serialize(),
type: "Post",
dataType: "Json",
success: function (result) {
if (result.Success) {
$("#dvgouropList").html(result.Html);
}
eval(result.Script);
},
error: function () {
alert("خطا!");
}
});
}
});
});
</script>
}




حالا قبل از اینکه اکشن اجرا بشه از طریق کد زیر چک میکنم که به صورت اجاکسی باشه

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MVC_Shop.Helper.Filter
{
public class AjaxOnlyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest() )
{
base.OnActionExecuting(filterContext);
}
else
{
filterContext.Result=new HttpNotFoundResult();
}

}
}
}

حالا مکل اینجاست که وقتی میاد از این فیلتر رد بشه میگه این اجاکسی نیست و خارج میشه
راهنمایی کنید

hamix666
جمعه 28 مهر 1396, 15:10 عصر
یعنی به صورت اجاکسی ارسال نمیشه خواهشا راهنمکایی کنید خیلی نیاز دارم

hamix666
جمعه 28 مهر 1396, 19:15 عصر
کسی نیست راهنمایی کنه؟

ali_md110
جمعه 28 مهر 1396, 22:52 عصر
type="submit" از خط زیر


<input type="submit" id="btnAddd" value="ثبت" class="btn btn-default" />


حذف کنید و در ادامش به تاپیک زیر مراجعه کنید
http://barnamenevis.org/showthread.php?543044 (http://barnamenevis.org/showthread.php?543044-%DA%86%DA%AF%D9%88%D9%86%D9%87-%D9%81%DB%8C%D9%84%D8%AA%D8%B1-ajaxonly-%D8%B1%D9%88-%D8%A7%D8%B9%D9%85%D8%A7%D9%84-%DA%A9%D9%86%D9%85%D8%9F%D8%9F%D8%9F)