简体   繁体   English

如何将参数从@ Url.Action传递到控制器功能

[英]how to pass parameter from @Url.Action to controller function

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函数。

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 });
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));
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})';

This way to pass value from Controller to View: 这种将值从Controller传递到View的方法:

ViewData["ID"] = _obj.ID;

Here is the way to pass value from View to Controller back: 这是将值从View传递回Controller的方法:

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

Try this: 尝试这个:

public ActionResult CreatePerson(int id) //controller 

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

it's working fine passing parameter. 它正在正常传递参数。

should you pass it in this way : 您是否应该以这种方式通过它:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});
@Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)

If you are using Url.Action inside JavaScript then you can 如果您在JavaScript中使用Url.Action ,则可以

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!");
  }
});

Try this 尝试这个

public 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。

Need To Two Or More Parameters Passing Throw view To Controller Use This Syntax... Try.. It. 需要两个或多个参数传递到控制器的Throw视图使用此语法... Try .. It。

var id=0,Num=254;var str='Sample';    
var Url = '@Url.Action("ViewNameAtController", "Controller", new RouteValueDictionary(new { id= "id", Num= "Num", Str= "str" }))'.replace("id", encodeURIComponent(id));
    Url = Url.replace("Num", encodeURIComponent(Num));
    Url = Url.replace("Str", encodeURIComponent(str));
    Url = Url.replace(/&amp;/g, "&");
window.location.href = Url;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM