我正在尝试管理每个页面的关键字。我有这门课
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>
但这是一个问题。当我提交表单时,它会转到关键字控制器,浏览器只显示部分视图,但我只想提交表单并停留在当前页面中。