我正在尝试使用Ajax.BeginForm将我的视图发回控制器,但它无法正常工作。我已尝试在stackoverflow上发布了一些建议,但它们都不起作用。以下是我的代码。
//视图
@using(Ajax.BeginForm("Index", "fundcode", new AjaxOptions() { HttpMethod = "POST" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="input-group">
@Html.HiddenFor(model => model.Id)
@Html.LabelFor(model => model.FundLocal, htmlAttributes: new { @class = "input-group-addon", @style = "font-weight: bold;" })
@Html.EditorFor(model => model.FundLocal, new { htmlAttributes = new { @class = "form-control" } })
</div>
<label class="text-danger pull-right">@Html.ValidationMessageFor(model => model.FundLocal)</label>
</div>
<br />
<div class="form-group">
<div class="input-group">
@Html.LabelFor(model => model.FundEnglish, htmlAttributes: new { @class = "input-group-addon", @style = "font-weight: bold;" })
@Html.EditorFor(model => model.FundEnglish, new { htmlAttributes = new { @class = "form-control" } })
</div>
<label class="text-danger pull-right">@Html.ValidationMessageFor(model => model.FundEnglish)</label>
</div>
<br />
<div class="form-group">
<div class="input-group">
@Html.LabelFor(model => model.FundCode1, htmlAttributes: new { @class = "input-group-addon", @style = "font-weight: bold;" })
@Html.EditorFor(model => model.FundCode1, new { htmlAttributes = new { @class = "form-control" } })
</div>
<label class="text-danger pull-right">@Html.ValidationMessageFor(model => model.FundCode1)</label>
</div>
<br />
<div class="form-group">
<input type="submit" id="btnSave" value="ثبت" class="btn btn-primary btn-flat col-lg-2 col-md-2 col-sm-2 col-xs-12 pull-right" />
</div>
}
//控制器操作
[HttpPost]
public ActionResult Index(Models.FundCode fundcode)
{
if (ModelState.IsValid)
{
var db = new Models.FAPEntities();
if (fundcode.Id == null)
{
fundcode.InsertDate = DateTime.Now;
fundcode.InsertedBy = 1;
fundcode.UpdatedBy = 0;
fundcode.DeletedBy = 0;
db.FundCodes.Add(fundcode);
}
else
{
var record = db.FundCodes.Find(fundcode.Id);
if (record != null)
{
record.UpdateDate = DateTime.Now;
record.FundLocal = fundcode.FundLocal;
record.FundEnglish = fundcode.FundEnglish;
record.FundCode1 = fundcode.FundCode1;
record.UpdatedBy = 1;
}
}
db.SaveChanges();
}
ActivateTab(classes.ActiveTab.AddTab);
ViewBag.FundCodes = new Models.FAPEntities().FundCodes.Where(f => f.DeletedBy == 0).OrderByDescending(f => f.Id).ToList();
return View();
}
//模型
public partial class FundCode
{
public decimal? Id { get; set; }
public Nullable<System.DateTime> InsertDate { get; set; }
public Nullable<System.DateTime> UpdateDate { get; set; }
public Nullable<System.DateTime> DeleteDate { get; set; }
[Required(ErrorMessage = "لطفا نام فند کود را دایر نماید")]
[DisplayName("نام فند کود (دری)")]
public string FundLocal { get; set; }
[Required(ErrorMessage = "لطفا نام فند کود را دایر نماید")]
[DisplayName("نام فند کود (انګیسی)")]
public string FundEnglish { get; set; }
[Required(ErrorMessage = "لطفا نام فند کود را دایر نماید")]
[DisplayName("فند کود")]
public string FundCode1 { get; set; }
public Nullable<decimal> InsertedBy { get; set; }
public Nullable<decimal> UpdatedBy { get; set; }
public Nullable<decimal> DeletedBy { get; set; }
}