Url.Action:如何将参数从视图传递到控制器? [英] Url.Action: How to Pass Parameter from View to Controller?

查看:120
本文介绍了Url.Action:如何将参数从视图传递到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Url.Action将参数从视图传递到Controller时遇到问题.

I'm having issues with passing a parameter from my View to Controller using Url.Action.

视图中的代码如下:

<form id="searchForm">
    <input type="text" name="search_criteria" />
    <input type="submit" value="Search" />
</form>

<div id="SearchDiv">

</div>

在我的脚本中:

$('#searchForm').submit(function (e) {
    e.preventDefault();

    $.ajax({
        url: '@Url.Action("Index_AddSearchCriteria", "Home", "search_criteria")',
        type: 'POST',
        dataType: 'html',
        success: function (result) {
            $('#SearchDiv').html(result);
        }
    })
})

在控制器中:

public PartialViewResult Index_AddSearchCriteria(string search_criteria)
        {
            ViewModel.SearchCriteria = search_criteria;

            return PartialView("SearchBar", ViewModel);
        }

我如何通过Url.Action发送参数,以便在Controller中将其作为参数接受?

How would I send a parameter through Url.Action so that it may be accepted as a parameter in the Controller?

推荐答案

您需要使用data:参数

var data = { search_criteria: $("search_criteria).val() }; // add id="search_criteria" to the textbox
$.ajax({
    url: '@Url.Action("Index_AddSearchCriteria", "Home")',
    type: 'POST',
    data: data,
    dataType: 'html',
    success: function (result) {
        $('#SearchDiv').html(result);
    }
})

或者您可以将其简化为

$('#SearchDiv').load('@Url.Action("Index_AddSearchCriteria", "Home")', { search_criteria: $("search_criteria).val() });

这篇关于Url.Action:如何将参数从视图传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆