此代码在局部视图中使用局部视图。在Create.cshtml中保存记录并在AwardsHonoursReceived.cshtml中显示,但是我遇到了一个问题,ajax工作正常,但在函数saveAwardHonour(tbl_EmpAwardsReceived obj)中的db(AwardHonoursRecieved.cs)中保存时显示错误警告< /强>
表示ajax在执行期间显示错误警告,但是记录保存但未执行(成功:功能(数据){$(&#34; #AgwardHonours&#34;)。html(&#34;&#34; ); $(&#34;#AwardHonours&#34;)。html(数据); },)
UserController中
public ActionResult Index()
{
return View();
}
public ActionResult AwardsHonoursReceived(int? EmployeeId)
{
AwardRecievedViewModel model = new AwardRecievedViewModel();
AwardHonoursRecieved.bindAwardHonourToList(model);
ViewBag.EmployeeId = EmployeeId;
return View(model);
}
Index.cshtml
@Html.Action("AwardsHonoursReceived", "User")
AwardsHonoursReceived.cshtml
@model HRMS.Models.UserModels.AwardRecievedViewModel
<div id="AwardHonours">
<script src="@Url.Content("~/Content/js/jquery-1.8.3.min.js")"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.CreateBtnAH').click(function () {
var EmpId = $("#EmployeeId").val();
var url = '@Url.Action("Create", "AwardHonours")?EmpId=' + EmpId;
$('#EditAH').html("");
$('#EditAH').load(url);
});
});
</script>
<section class="panel">
<span id="EditAH"></span>
<input type="hidden" id="EmployeeId" name="EmployeeId" value="@ViewBag.EmployeeId" />
<div class="text-right"><span class="CreateBtnAH btn btn-info">Create New</span></div>
<div class="panel-body">
<div class="adv-table">
<div id="dynamic-table_wrapper" class="dataTables_wrapper form-inline" role="grid">
<div class="panel-body">
<section id="no-more-tables">
<table class="table table-bordered table-striped table-condensed table-responsive cf">
<thead class="cf">
<tr>
<th>Name Of The Award</th>
<th class="numeric">Organization</th>
<th class="numeric">Date</th>
<th class="numeric">Venue</th>
<th class="numeric">Purpose Of The Order</th>
<th class="numeric">Action</th>
</tr>
</thead>
<tbody>
@if (Model.AwardRecievedViewModelList != null)
{
foreach (var item in Model.AwardRecievedViewModelList)
{
<tr>
<td data-title="Code">@Html.DisplayFor(modelItem => item.NameOfAward)</td>
<td data-title="Code">@Html.DisplayFor(modelItem => item.Organization)</td>
<td data-title="Code">@Html.DisplayFor(modelItem => item.Date)</td>
<td data-title="Company">@Html.DisplayFor(modelItem => item.Venue)</td>
<td data-title="Code">@Html.DisplayFor(modelItem => item.PurposeOfAward)</td>
<td data-title="Volume" class="numeric">
@Html.HiddenFor(modelItem => item.EmployeeId)
<span><a href="#" id="@item.Id" class="EditBtnAH">
<img src="@Url.Content("~/Content/images/edit.png")" alt="edit" /></a></span>
<span><span id="@item.Id" class="DeleteBtnAH">
<img src="@Url.Content("~/Content/images/delete.png")" alt="Delete" /></span></span>
</td>
</tr>
}
}
else
{
<tr>
<td colspan="6">NO Records</td>
</tr>
}
</tbody>
</table>
</section>
</div>
<div class="row">
<div class="col-md-5">
<div class="dataTables_info" id="dynamic-table_info">Showing 1 to 10 of 57 entries</div>
</div>
<div class="col-md-7">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled"><a href="#">← Previous</a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li class="next"><a href="#">Next → </a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
AwardHonours Controller
public ActionResult Create(int? EmpId)
{
ViewBag.Emp = EmpId;
return View();
}
[HttpPost]
public ActionResult Create(AwardRecievedViewModel model)
{
if (ModelState.IsValid)
{
using (HatronEntities context = new HatronEntities())
{
tbl_EmpAwardsReceived obj = new tbl_EmpAwardsReceived();
obj.NameOfAward = model.NameOfAward;
obj.Organization = model.Organization;
obj.Date = model.Date;
obj.Venue = model.Venue;
obj.PurposeOfAward = model.PurposeOfAward;
obj.EmployeeId = model.EmployeeId;
AwardHonoursRecieved.saveAwardHonour(obj);
}
var msg = "Created Successfully";
return RedirectToAction("AwardsHonoursReceived", "User", new { msg = msg, EmployeeId = model.EmployeeId });
}
else {
var msg = "Not Saved";
return RedirectToAction("AwardsHonoursReceived", "User", new { msg = msg }); }
}
create.cshtml
<div class="panel-body">
<form class="form-horizontal bucket-form" id="myformAH">
@Html.ValidationSummary(true)
<div class="form-group">
<label class="col-sm-3 control-label">Name Of Award</label>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.NameOfAward, new { id = "", @class = "form-control" })
@Html.ValidationMessageFor(model => model.NameOfAward)
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Organization</label>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.Organization, new { id = "", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Organization)
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Date</label>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.Date, new { id = "", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Date)
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Venue</label>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.Venue, new { id = "", @class = "form-control" })
@Html.ValidationMessageFor(model => model.Venue)
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Purpose Of Award</label>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.PurposeOfAward, new { id = "", @class = "form-control" })
@Html.ValidationMessageFor(model => model.PurposeOfAward)
</div>
</div>
<input type="hidden" name="EmployeeId" value="@ViewBag.Emp" />
<div class="form-group has-error">
<label class="col-sm-3 control-label col-lg-3" for="inputError"> </label>
<div class="col-lg-6">
<input type="hidden" id="SubModuleId" name="SubModuleId" value="@ViewBag.SubModuleId" />
<input type="submit" value="Save & Exit" class="submitAH btn btn-info" />
<input type="button" value="Cancel" class="cancelAH btn btn-info" />
</div>
</div>
</form>
</div>
<script src="@Url.Content("~/Content/js/jquery-1.8.3.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"> </script>
<script type="text/javascript">
$(document).ready(function () {
$('.cancelAH').click(function () {
$('#EditAH').html("");
});
$('.submitAH').click(function () {
var Form = $("#myformAH").serialize();
if ($("#myformAH").valid()) {
$.ajax({
url: "/AwardHonours/Create",
type: 'POST',
data: Form,
success: function (data) {
$("#AwardHonours").html("");
$("#AwardHonours").html(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
return false;
}
});
}
});
});
</script>
AwardHonoursRecieved.cs
public static int saveAwardHonour(tbl_EmpAwardsReceived obj)
{
int status = 0;
int lastID = 0;
using (HatronEntities context = new HatronEntities())
{
//==== Check for duplicacy.
// status = context.tbl_EmpPromotion.Count(r => r.Designation == obj.Designation);
//if (status == 0) //==== means no similar Relation exists save Relation.
// {
// Fetch Id of last record from table
var result = (from c in context.tbl_EmpAwardsReceived.OrderByDescending(u => u.Id)
select new { Id = c.Id }).FirstOrDefault();
if (result != null)
{
lastID = Convert.ToInt32(result.Id);
}
obj.Id = lastID + 1;
context.tbl_EmpAwardsReceived.Add(obj);
context.SaveChanges();
// }
}
return status;
}