반응형
@ Url.Action에서 컨트롤러 함수로 매개 변수를 전달하는 방법
나는 기능을 가지고 CreatePerson(int id)내가 전달하려는, id에서 @Url.Action.
다음은 참조 코드입니다.
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person") + id";
위의 코드 id는 CreatePerson함수에 값을 전달하지 못합니다 .
다음과 같이 전달할 수 있습니다.
Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));
또는이 방법으로 전달할 수도 있습니다.
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;
또는
var id = 'some value';
window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';
Controller에서 View로 값을 전달하는 방법은 다음과 같습니다.
ViewData["ID"] = _obj.ID;
View에서 Controller로 값을 다시 전달하는 방법은 다음과 같습니다.
<input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />
이 시도:
public ActionResult CreatePerson(int id) //controller
window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";
매개 변수를 전달하는 것이 잘 작동하고 있습니다.
다음과 같이 전달해야합니다.
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)
Url.ActionJavaScript 내부 를 사용하는 경우 다음을 수행 할 수 있습니다.
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 .. 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(/&/g, "&");
window.location.href = Url;
이 시도
public ActionResult CreatePerson(string Enc)
window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&", "&");
Enc 문자열 내에서 ID를 얻습니다.
반응형
'Program Club' 카테고리의 다른 글
| ASP.NET MVC 컨트롤러 수명주기 (0) | 2020.11.24 |
|---|---|
| requestAnimationFrame 재귀 / 루프를 중지하는 방법은 무엇입니까? (0) | 2020.11.23 |
| CodeIgniter의 뷰 내에 뷰를 포함하는 가장 좋은 방법 (0) | 2020.11.23 |
| C ++에서 std :: vector를 반환하는 효율적인 방법 (0) | 2020.11.23 |
| JAXB : XML 문서를 비 정렬 화하는 동안 네임 스페이스를 무시하는 방법은 무엇입니까? (0) | 2020.11.23 |