【问题标题】:How to give inputs as a parameter to an action method in MVC5?如何将输入作为参数提供给 MVC5 中的操作方法?
【发布时间】: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


【解决方案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>

【讨论】:

  • 如果我将 url 部分保留在您的答案中,它无法找到它,因为它试图解析当前 url:localhost:44300/@Url.Action(%22ServiceRequest%22,%20%22Home%22),但如果我将其更改为 /Home/ServiceRequest,那么我在控制台中出现另一个错误,说没有找到元素。有什么想法吗?
  • @ett - 很抱歉无法及时回复您。你还在面对这个问题吗?我提供的代码很适合我在您的原始帖子中使用动作签名。
  • 是的,我在上面的评论中写了这个问题。有什么想法吗?
  • @ett - 我不确定你为什么会遇到这个问题。似乎有一些参考或js问题。我正在更新在我的机器上运行的完整代码,看看它是否有帮助。
  • 可能是因为我的代码中有问题的视图是 _Layout.cshtml 文件?
【解决方案2】:

请尝试以下代码标记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....

【讨论】:

  • 我在我的_Layout里面,所以你可以假设我没有分配给它的模型。
【解决方案3】:

您必须为您的输入添加名称属性,例如:

<div class="input-top">

 <input id="homeZipCode" name="ZipCode /*Your property Name*/" ...>
 <input id="homeService" name="ServiceName"  ...>
<input type="submit" value="ServiceRequest">
</div>

或者使用 html helpers

@using (Html.BeginForm())
{
    @Html.TextBoxFor(model => model.ZipCode)
     @Html.TextBoxFor(model => model.ServiceName)
    <input type="submit" value="ServiceRequest">
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多