在我_Layout.cshml文件,我有以下代码段:如何使用MVC5中的数据重定向到新视图?
<div class="input-top">
<a class="GoBtn" href=""><img src="~/Content/img/GOBtn.png" class="gobtn-position"></a>
<input id="homeZipCode" type="text" class="form-control input-position-2" placeholder="ZIP">
<input id="homeService" type="text" class="form-control input-position-1" placeholder="What do you need done today?">
</div>
而且一点是,我想上面的两个输入字段的值,如果href重定向到一个新观点属性被点击,但是当它重定向并打开新视图时,在新视图中,在特定的两个输入字段而不是空白处,我希望这些数据被呈现,我的意思是要写入的文本。在我重定向观点,我有这样的事情:
<div class="form-group">
@Html.LabelFor(model => model.ZipCode, "Zip", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ZipCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ServiceName, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ServiceName, new { htmlAttributes = new { @class = "form-control", @id = "service-manual" } })
@Html.ValidationMessageFor(model => model.ServiceName, "", new { @class = "text-danger" })
</div>
</div>
出于这个原因,我试着写一个AJAX调用,并把它放在我的_Layout.cshtml文件,但它似乎不工作。下面你可以找到我的AJAX调用:
$(document).ready(function() {
$('a.GoBtn').on('click', function (e) {
e.preventDefault();
var homeZipCode = $("#homeZipCode").val();
var homeService = $("#homeService").val();
var model = { StateID: 1, ZipCode: homeZipCode, ServiceName: homeService };
$.ajax({
url: '@Url.Action("ServiceRequest", "Home")',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'html',
data: JSON.stringify(model)
})
.success(function (result) {
});
});
});
的问题是,当我点击href属性,视图不会重定向,我还是那个我点击HREF的看法。可能是什么问题?
编辑:
public class RequestViewModel
{
[Required(ErrorMessage = "Please select a state")]
[Display(Name = "State")]
public int StateID { get; set; }
[Required(ErrorMessage = "Please enter a zip code")]
[Display(Name = "Zip")]
public string ZipCode { get; set; }
[Required(ErrorMessage = "Please choose a service")]
[Display(Name = "Service")]
public string ServiceName { get; set; }
}
而且我的控制器:
[Authorize]
public ActionResult ServiceRequest()
{
ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
return View();
}
[Authorize]
[HttpPost]
public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null)
{ ... }
我的输入位于我的_Layout.cshtml文件中,我想重定向到实际使用模型的不同视图。 – tett 2014-10-18 22:53:34