【发布时间】:2014-10-06 11:28:37
【问题描述】:
我有一个 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.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>
我写了一个类似这样的代码,但它在 new { rvm.ZipCode = homeZipCode, rvm.ServiceName = homeService } 的部分显示错误,我可以看到关键是我的 _Layout.cshtml 文件中的任何地方都没有定义 rvm,因此它不能解析 ZipCode 和 ServiceName 属性。知道吗,如何将这两个输入字段的值传递给我的操作方法?
【问题讨论】:
-
ServiceRequest()是 GET 还是 POST? -
我有该操作方法的 GET 和 POST 版本。
-
一个动作链接是一个 GET 但你已经包含了你的 POST 方法。你的GET方法是
public ActionResult ServiceRequest(string homeZipCode, string homeService)吗?
标签: c# html asp.net-mvc razor