如何将输入作为参数提供给MVC5中的操作方法
本文关键字:MVC5 操作方法 参数 输入 | 更新日期: 2023-09-27 18:25:38
我有一个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文件中,我有两个输入字段和一个"a href",当有人按下包含href的对象时,我想接受这两个字段的输入,并调用上面的操作方法。这是我的_Layout.chtml.中的部分
<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 }
的部分显示了错误,我可以看到,关键是在我的_Layout.cshtml文件中的任何地方都没有定义rvm,因此它无法解析ZipCode和ServiceName属性。知道吗,我如何将这两个输入字段的值传递给我的操作方法?
我认为您可以为锚点标记定义一个点击处理程序,并传递一个带有数据的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>
请尝试以下标记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....
您必须为您的输入添加名称属性,如:
<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">
}