如何从@Url传递参数。在asp.net mvc3中的控制器操作

时间:2022-11-30 21:13:31

I have a function CreatePerson(int id) , I want to pass id from @Url.Action.

我有一个函数CreatePerson(int id),我想从@Url.Action传递id。

Below is the reference code:

以下是参考代码:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person") + id";

the above code fails to pass id value to CreatePerson function.

上面的代码没有将id值传递给CreatePerson函数。

8 个解决方案

#1


95  

you can pass it this way :

你可以这样传递:

Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));

OR can also pass this way

或者也可以通过这种方式。

Url.Action("CreatePerson", "Person", new { id = id });

#2


12  

public ActionResult CreatePerson (string id)

window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID",id);

public ActionResult CreatePerson (int id)

window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID", parseInt(id));

#3


2  

This way to pass value from Controller to View:

通过这种方式将值从控制器传递到视图:

ViewData["ID"] = _obj.ID;

Here is the way to pass value from View to Controller back:

下面是从视图到控制器的传递值的方法:

<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />

#4


0  

should you pass it in this way :

你是否应该这样传递:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});

#5


0  

@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)

#6


0  

If you are using Url.Action inside JavaScript then you can

如果您正在使用Url。然后在JavaScript内执行操作

var personId="someId";
$.ajax({
  type: 'POST',
  url: '@Url.Action("CreatePerson", "Person")',
  dataType: 'html',
  data: ({
  //insert your parameters to pass to controller
    id: personId 
  }),
  success: function() {
    alert("Successfully posted!");
  }
});

#7


0  

try this

试试这个

public ActionResult CreatePerson(string Enc)

公共ActionResult CreatePerson(string Enc)

 window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&amp;", "&");

you will get the id inside the Enc string.

您将在Enc字符串中获得id。

#8


-1  

public ActionResult CreatePerson(int id) //controller 

window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;

Or

var id = 'some value';
window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';

#1


95  

you can pass it this way :

你可以这样传递:

Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));

OR can also pass this way

或者也可以通过这种方式。

Url.Action("CreatePerson", "Person", new { id = id });

#2


12  

public ActionResult CreatePerson (string id)

window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID",id);

public ActionResult CreatePerson (int id)

window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID", parseInt(id));

#3


2  

This way to pass value from Controller to View:

通过这种方式将值从控制器传递到视图:

ViewData["ID"] = _obj.ID;

Here is the way to pass value from View to Controller back:

下面是从视图到控制器的传递值的方法:

<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />

#4


0  

should you pass it in this way :

你是否应该这样传递:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});

#5


0  

@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)

#6


0  

If you are using Url.Action inside JavaScript then you can

如果您正在使用Url。然后在JavaScript内执行操作

var personId="someId";
$.ajax({
  type: 'POST',
  url: '@Url.Action("CreatePerson", "Person")',
  dataType: 'html',
  data: ({
  //insert your parameters to pass to controller
    id: personId 
  }),
  success: function() {
    alert("Successfully posted!");
  }
});

#7


0  

try this

试试这个

public ActionResult CreatePerson(string Enc)

公共ActionResult CreatePerson(string Enc)

 window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&amp;", "&");

you will get the id inside the Enc string.

您将在Enc字符串中获得id。

#8


-1  

public ActionResult CreatePerson(int id) //controller 

window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;

Or

var id = 'some value';
window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';