【问题标题】:post a form using ajax inside a partial view which loaded on another view在加载到另一个视图的局部视图中使用 ajax 发布表单
【发布时间】:2013-06-11 09:25:06
【问题描述】:

我正在尝试管理每个页面的关键字。我有这门课

 public class Keyword
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Display(Name = "آدرس")]
    public string Address { get; set; }

    [Display(Name = "کلمات کلیدی")]
    public string KeyWords { get; set; }

    [Display(Name = "شرح صفحه")]
    public string Description { get; set; }
}

这是我的控制器

[Authorize(Roles = "admins")]
    public ActionResult Create(string Address="")
    {
        Keyword keyword = db.Keywords.Where(k => k.Address == Address).FirstOrDefault();
        if (null == keyword)
        {
            keyword = new Keyword() { Address = Address };
        }
        return PartialView(keyword);
    } 

    [HttpPost]
    [Authorize(Roles = "admins")]
    public ActionResult Create(Keyword keyword)
    {
        keyword.Address = Request.UrlReferrer.PathAndQuery;
        if (db.Keywords.Count(k => k.Address == keyword.Address) > 0)
        {
            var model = db.Keywords.Where(k => k.Address == keyword.Address).FirstOrDefault();
            model.Description = keyword.Description;
            model.KeyWords = keyword.KeyWords;
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                //return Redirect(Request.UrlReferrer.AbsoluteUri);
            }
        }
        else if (ModelState.IsValid)
        {
            db.Keywords.Add(keyword);
            db.SaveChanges();
            //return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        return PartialView(keyword);
    }

这是局部视图

@model Stockala.Models.Keyword

@using (Ajax.BeginForm("Create", "Keyword", new AjaxOptions() { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "keywords-div" }))
{
    @Html.ValidationSummary(true)
    @Html.AntiForgeryToken()
    <fieldset>
        <legend>کلمات کلیدی</legend>
            <table class="fields">
        <tr>
            <td class="label">
                @Html.LabelFor(model => model.KeyWords) :
            </td>
            <td class="editor-field">
                @Html.TextBoxFor(model => model.KeyWords, new { style = "width:600px;" })
                @Html.ValidationMessageFor(model => model.KeyWords)
            </td>
        </tr>
        <tr>
            <td class="label">
                @Html.LabelFor(model => model.Description) :
            </td>
            <td class="editor-field">
                @Html.TextAreaFor(model => model.Description, new { style = "width:600px;" })
                @Html.ValidationMessageFor(model => model.Description)
            </td>
        </tr>
        <tr>
            <td colspan="2">
            <p>
                <input id="keysubmit" type="submit" value="تایید" class="button"/>
            </p>
            </td>
        </tr>
    </table>    
    </fieldset>
}

这就是我在 _layout.chtml 中渲染部分视图的方式

<div id="main">
        @RenderBody()
        @if (User.IsInRole("admins"))
        {
            <div id="keywords-div">
                @Html.Action("Create", "Keyword", new { Address = Request.Url.PathAndQuery })
            </div>
        }
    </div>

但这是一个问题。当我提交表单时,它会转到关键字控制器,浏览器只显示部分视图,但我只想提交表单并留在当前页面。

【问题讨论】:

    标签: asp.net-mvc-3 jquery post partial-views


    【解决方案1】:

    这种方式对我有用。适应您的情况:

    @model Stockala.Models.Keyword
    
    @using (Ajax.BeginForm("Create", "Keyword", new AjaxOptions { HttpMethod = "POST", InsertionMode     = InsertionMode.Replace, UpdateTargetId = "keywords-div" }))
    {
    <div id="keywords-div">
        //Rest of the form body
    </div>
    }
    

    从“新 AjaxOptions ()”中删除 '()'

    【讨论】:

    • 谢谢兄弟。它没有用。您是否添加了对 jquery.validate.unobtrusive.min 或其他脚本等脚本的引用?
    • 我使用了本页中描述的方式,它对我有用stackoverflow.com/questions/10317141/…
    • 你能用页面中加载的脚本更新你的问题吗?
    猜你喜欢
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多