我是MVC 4的新手。请问大家。请帮我解决以下问题。 我有一个索引操作方法,我在其中设置分页后要显示的记录数。
索引操作方法如图所示
public ActionResult Index(int? page){
Provider providerList = new Provider();
IList<Provider> providers = DAL.GetListofProviders.ToList();
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(providers.ToPagedList(pageNumber,pageSize));
}
索引视图如下所示。
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<DEMO_JAN14.Models.Provider>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<div class="container">
<h1>Providers List</h1><br/>
<a class="btn btn-primary col-sm-1" href="/Provider/Create">Create</a><br/>
<table class="table table-bordered">
<tr>
<th>Provider Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Certification</th>
<th></th>
</tr>
<tbody data-bind="foreach:viewModel">
<tr>
<td class="col-lg-2" data-bind="text: ProviderType"></td>
<td class="col-lg-2" data-bind="text: FirstName"></td>
<td class="col-lg-2" data-bind="text: LastName"></td>
<td><a class=" col-lg-2 btn btn-danger" id="del" onclick="" data-bind="attr: { href: '/Provider/Delete/'+ ProviderID }">Delete</a>
</td>
</tr>
</tbody>
</table>
Page @(Model.PageCount<Model.PageNumber? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
</div>
</body>
<script type="text/javascript" src="../../Scripts/knockout.mapping-latest.js"></script>
<script type="text/javascript">
var data = @Html.Raw(Json.Encode(Model));
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
</script>
通过单击“创建”按钮,用户将被定向到另一个页面,在该页面中将数据输入表单并发布。
发布脚本如图所示。
$("#Submit").on("click", function () {
if (ValidatethisForm()) {
$.ajax({
type: "POST",
url: "/Provider/Create",
data: Provider
});
}
});
在Create Action中,我错误地重定向到Index方法。
public ActionResult Create(Provider provider)
{
try
{
DAL.InsertDataintoProviderTable(provider.ProviderType,provider.FirstName,provider.LastName);
return RedirectToAction("Index(1)","Provider");
}
catch
{
return View();
}
}
请指引我正确的方向。谢谢。
编辑 ValidatethisForm方法
function ValidatethisForm() {
if (Provider.ProviderType() === "")
return false;
else if (Provider.FirstName() === "")
return false;
else if (Provider.LastName() === "")
return false;
else if (Provider.Certification() === "")
return false
else if (Provider.Specialization() === "")
return false;
else if (Provider.ContactNumber() === "")
return false;
else if (Provider.ContactEmail() === "")
return false;
else if (Provider.FacilityName() === "")
return false;
else
return true;
}
创建页面的HTML
@model DEMO_JAN14.Models.Provider
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>CREATE PROVIDER</title>
</head>
<body>
<div class="container">
<h1 class="col-sm-offset-2">Enter Provider Details:</h1><br />
<form class="form-horizontal" role="form" id="ProviderDetailsForm">
<div class="form-group">
<label class="col-sm-2 control-label labelfont">PROVIDER TYPE:</label>
<div class="col-sm-6" id="ProviderType">
<select class="form-control" data-bind="value: ProviderType" autofocus="autofocus">
<option>Select a Provider Type</option>
<option>Doctor</option>
<option>Facility</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">FIRST NAME:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Enter the First Name" id="FirstName" data-bind="value: FirstName" onkeypress="return onlyAlphabets(event);">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_FirstName">Enter the first name</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">LAST NAME:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Enter the Last Name" id="LastName" data-bind="value: LastName" onkeypress="return onlyAlphabets(event);">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_LastName">Enter the first name</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">CERTIFICATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Certification" data-bind="value: Certification">
<option>Select a Certification</option>
<option>Doctor</option>
<option>Facility</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">SPECIALIZATION:</label>
<div class="col-sm-6">
<select class="form-control" id="Specialization" data-bind="value: Specialization">
<option>Select a Specialization</option>
<option>Doctor</option>
<option>Facility</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">SSN:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="Enter the SSN" id="SSN" d data-bind="value: SSN" onkeypress="return onlyNumbers(event);" maxlength="9">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_SSN">Enter the SSN</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">FACILITY NAME:</label>
<div class="col-sm-6">
<input type="text" class="form-control" data-bind="value: FacilityName" placeholder="Enter the Facility Name" id="FacilityName">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_FacName">Enter the Facility name</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">CONTACT NUMBER:</label>
<div class="col-sm-6">
<input type="text" class="form-control" data-bind="value: ContactNumber" placeholder="Enter the Contact Number" id="ContactNumber" onkeypress="return onlyNumbers(event);" maxlength="10">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_ContactNum">Enter the Contact Number</label>
</div>
<div class="form-group">
<label class="col-sm-2 control-label labelfont">EMAIL ADDRESS:</label>
<div class="col-sm-6">
<input type="text" class="form-control" data-bind="value: ContactEmail" placeholder="Enter your email address" id="EmailAddress">
</div>
<label class="col-sm-4 labelfont errorMsg" id="Err_EmailAddress">Enter the Email Address</label>
</div>
<div class="form-group">
<button type="submit" id="Submit" class="btn btn-primary col-sm-1 col-sm-offset- 4">Submit</button>
<button type="reset" class="btn btn-primary col-sm-1">Reset</button>
</div>
</form>
</div>
</body>
<script type="text/javascript" src="../../Scripts/Create_Script.js"></script>
答案 0 :(得分:0)
问题是你用ajax调用页面,重定向不能像那样工作。您需要通过将URL传递给它来从JavaScript执行重定向。
在您的操作方法中,返回您要重定向到的操作的网址:
public ActionResult Create(Provider provider)
{
try
{
DAL.InsertDataintoProviderTable(provider.ProviderType,provider.FirstName,provider.LastName);
string url = Url.Action("Index", "Provider");
return Json(new {url});
}
catch
{
return View();
}
}
在JavaScript中抓取该网址并重定向到它:
$("#Submit").on("click", function (e) {
e.preventDefault();
if (ValidatethisForm()) {
$.ajax({
type: "POST",
url: "/Provider/Create",
data: Provider,
success: function(result) {
if (result.url) {
location.href = result.url;
}
}
});
}
});