将多个参数传递给控制器​​?

时间:2009-07-14 15:23:21

标签: asp.net-mvc

确定。简单的一个包裹着我的大脑

我在控制器中有一个方法

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}

如何从URL到控制器获取多个参数?

我不想使用QueryString,因为它似乎是非mvc思维集。

有路线吗?或其他机制使这项工作?或者我在MVC

中遗漏了一些altogehter

修改

我正在尝试的网址是

http://site.com/search/details/FirstName and Surname

所以如果这是经典的asp

http://site.com/search/details?FirstName+Surname

但是我觉得我错过了一些在我急于获得工作代码的东西,我错过了在put请求中确实应该存在的一点 - 我应该从formcollection中收集它。

虽然看看是否可以这样做可能值得 - 以备将来参考=>

5 个答案:

答案 0 :(得分:9)

例如,假设您有一个计算两点之间距离的动作方法:

public void Distance(int x1, int y1, int x2, int y2)
{
   double xSquared = Math.Pow(x2 - x1, 2);
   double ySquared = Math.Pow(y2 - y1, 2);
   Response.Write(Math.Sqrt(xSquared + ySquared));
}

仅使用默认路由,请求必须如下所示:

/simple/distance?x2=1&y2=2&x1=0&y1=0

我们可以通过定义一个允许您以更干净的格式指定参数的路线来改进这一点。

RegisterRoutes内的Global.asax.cs方法中添加此代码。

routes.MapRoute("distance",
"simple/distance/{x1},{y1}/{x2},{y2}",
new { Controller = "Simple", action = "Distance" }
);

我们现在可以使用/simple/distance/0,0/1,2

来调用它

答案 1 :(得分:2)

这样的东西?:

routes.MapRoute("TheRoute",
    "{controller}/{action}/{strFirstName}/{strLastName}",
    new { controller = "Home", action = "Index", strFirstName = "", strLastName = "" }
);

或:

routes.MapRoute("TheRoute2",
    "people/details/{strFirstName}/{strLastName}",
    new { controller = "people", action = "details", strFirstName = "", strLastName = "" }
);

<强>更新:

此路线应放在“默认”路线之前:

// for urls like http://site.com/search/details/FirstName/Surname
routes.MapRoute("TheRoute",
    "search/details/{strFirstName}/{strLastName}",
    new { controller = "search", action = "details", strFirstName = "", strLastName = "" }
);

routes.MapRoute("Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

答案 2 :(得分:0)

也可以使用FormCollection:

public ActionResult Details(int listId, FormCollection form)
{
  return View(rep.getList(form["firstName"], form["lastName"])
}

同样,如果HTTP请求包含具有完全相同名称的表单值(区分大小写),则它将自动传递到ActionResult方法。

另外,为了清楚起见,没有关于查询字符串参数的非MVC。

答案 3 :(得分:0)

在表单中使用隐藏值

<%= Html.Hidden("strFirstName", Model.FirstName)%>
<%= Html.Hidden("strLastName", Model.LastName)%>

并且模型绑定器将执行绑定

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}

答案 4 :(得分:0)

我也遇到过同样的问题,我所做的就是在jQuery函数中使用Ajax调用。首先,我使用jQuery选择器选择了所有参数值。下面是我的jQuery函数。

<script language="javascript" type="text/javascript"> 
 $(document).ready(function () {
    $('#btnSendNow').click(function () {
    var grid = $('#Patient-kendo-Grid').data('kendoGrid');
    var location = $('#EmailTempalteLocation option:selected').text();
    var appoinmentType = $('#EmailTemplateAppoinmentType option:selected').text();
    var emailTemplateId = $('#EmailTemplateDropdown').val();
    var single = $('input:radio[name=rdbSingle]:checked').val();
    var data = grid.dataSource.view();
    var dataToSend = {
    patients: data,
    place: location,
    appoinment: appoinmentType,
    rdbsingle: single,
    templateId: emailTemplateId
    };
    debugger;
    $.ajax({
    url: 'Controller/Action',
    type: 'post',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(dataToSend)
    });
    });
    });
</script>

我的控制器方法有五个参数,如下所示。

[HttpPost]
public ActionResult SendEmailToMany(List<PatientModel> patients, string place, string appoinment, string rdbsingle, string templateId)
{ 
emailScheduleModel = new EmailScheduleModel();
AmazonSentEmailResultModel result;
List<string> _toEmailAddressList = new List<string>();
List<string> _ccEmailAddressList = new List<string>();
List<string> _bccEmailAddressList = new List<string>();
IEmailTemplateService emailTemplateService = new EmailTemplateService();
EmailTemplateContract template = emailTemplateService.GetEmailTemplateById(new       Guid(templateId));
emailScheduleModel.EmailTemplateContract = new EmailTemplateContract();
emailScheduleModel.EmailTemplateContract = template;
}

我的开发工作正常。

有关详细信息,请按照以下网址进行操作 http://dushanthamaduranga.blogspot.com/

相关问题