Asp.net mvc 3 在加载到另一个视图的局部视图中使用ajax发布表单
我试图管理每个页面的关键字。我有这门课Asp.net mvc 3 在加载到另一个视图的局部视图中使用ajax发布表单,asp.net-mvc-3,jquery,post,partial-views,Asp.net Mvc 3,Jquery,Post,Partial Views,我试图管理每个页面的关键字。我有这门课 public class Keyword { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] public int ID { get; set; } [Display(Name = "آدرس")] public string Address { get
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>
但问题出在这里。当我提交表单时,它会转到关键字控制器,浏览器只显示部分视图,但我只想提交表单并停留在当前页面。这种方式对我很有效。适应您的情况:
@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或其他脚本的任何引用?我使用了本页中描述的方法,它对我起到了作用。您是否可以使用页面中加载的脚本更新您的问题?