我有一个MVC5应用程序,我有一个像这样定义的视图模型:
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; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
public byte[] VideoData { get; set; }
public string VideoMimeType { get; set; }
}
我从这样定义的动作中调用此视图模型:
public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null)
{
...
}
所以,关键是在我的_Layout.cshtml文件中我有两个输入字段,一个"一个href",就是当有人按下包含href的对象时,我想拿这两个字段的输入,并调用上面的操作方法。这是我的_Layout.cshtml中的部分。
<div class="input-top">
<a href='@Url.Action("ServiceRequest", "Home", new { rvm.ZipCode = homeZipCode, rvm.ServiceName = homeService })'><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>
我写了一个像这样的代码,但是它显示了新{ rvm.ZipCode = homeZipCode, rvm.ServiceName = homeService }
部分的错误,我可以看到关键是rvm没有在我的_Layout.cshtml文件中的任何地方定义,因此它无法解析ZipCode和ServiceName属性。任何想法,我如何将这两个输入字段的值传递给我的动作方法?
答案 0 :(得分:1)
我认为您可以为锚标记定义单击处理程序,并传递带有数据的Ajax请求。这会更容易。
例如:
控制器:
public class ServiceController : Controller {
public ActionResult ServiceRequest()
{
return View();
}
[HttpPost]
public ActionResult ServiceRequest(RequestViewModel rvm, HttpPostedFileBase image = null, HttpPostedFileBase video = null)
{
throw new NotImplementedException();
}}
示例视图
@{ ViewBag.Title = "Index"; } <script src="~/Scripts/jquery-1.8.2.min.js"></script>
<div class="input-top">
<a class="Gobtn" href="#"><img src="~/Content/themes/base/images/ui-bg_flat_0_aaaaaa_40x100.png" /></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>
<script>
$(document).ready(function() {
$('a.Gobtn').on('click', function (e) {
e.preventDefault();
alert('here in');
debugger;
// Send an ajax post request with data
var homeZipCode = $("#homeZipCode").val();
var homeService = $("#homeService").val();
var model = { ZipCode: homeZipCode, ServiceName: homeService };
$.ajax({
url: '@Url.Action("ServiceRequest", "Service")',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'html',
data: JSON.stringify(model)
})
.success(function(result) {
});
});
});
</script>
答案 1 :(得分:0)
请尝试下面的代码a
<a href='@Url.Action("ServiceRequest", "Home", new { homeZipCode = Model.ZipCode, homeService = Model.ServiceName })'><img src="~/Content/img/GOBtn.png" class="gobtn-position"></a>
'rvm' has no meaning here....
答案 2 :(得分:0)
您必须为输入添加名称属性,例如:
<div class="input-top">
<input id="homeZipCode" name="ZipCode /*Your property Name*/" ...>
<input id="homeService" name="ServiceName" ...>
<input type="submit" value="ServiceRequest">
</div>
或者使用html助手
@using (Html.BeginForm())
{
@Html.TextBoxFor(model => model.ZipCode)
@Html.TextBoxFor(model => model.ServiceName)
<input type="submit" value="ServiceRequest">
}