뷰 내에서 Html.Action을 사용할 때 선택적 쿼리 매개 변수가 자식 작업에 나타나는 이유는 무엇입니까?

밀가루

MVC 응용 프로그램에는 두 가지 작업 방법이 있습니다. 하나는 레이아웃의 일부를 표시하고 그 중간에 다른 작업 (항목 목록 렌더링)을 렌더링하려고합니다. 편의상 첫 번째 작업을 ajax하면 대신 두 번째 작업의 결과를 얻도록 만들었습니다.

코드는 다음과 같습니다.

public ActionResult Action1(int? foo = null, int? bar = null, string search = null)
{
    ViewBag.Foo = foo;
    ViewBag.Bar = bar;
    ViewBag.Bar = search;
    if (Request.IsAjaxRequest() || this.ControllerContext.IsChildAction)
    {
        return Action2(foo, bar, search);
    }
    var model = GetSomeData();
    return View(model);
}

public ActionResult Action2(int? foo = null, int? bar = null, string search = null)
{
    ViewBag.Foo = foo;
    ViewBag.Bar = bar;
    ViewBag.Bar = search;
    var model = GetSomeOtherData(foo, bar, search);
    return View();
}

그런 다음 Action1에 대한 보기 에서 :

@model SomeClass
<div>
    Some layout and stuff from the model goes here
    @Html.Action("Action2", new { foo = ViewBag.Foo, bar = ViewBag.Bar })
</div>

그리고 보기 조치 2에 대한 :

@model IEnumerable<SomeOtherClass>
<div>
    <form action="@Url.Action("Action1", new { foo = ViewBag.Foo, bar = ViewBag.Bar })">
        <input type="text" name="search"/>
        <input type="submit" value="Search"/>
    </form>
    Here we render a list of items from the model
</div>

foo둘 다 barnull이고 I ajax Action1이거나 Action2 만 요청하면 양식의 URL에 null 인 쿼리 매개 변수가 포함되지 않습니다. Action1을 정상적으로 요청하면 문제가 나타납니다 (Ajax를 통하지 않음). foo둘 다 barnull 인 경우에도 URL에 쿼리가 포함 ?foo=&bar=됩니다..

이런 일이 발생 하는지 및 / 또는 발생 을 방지하는 방법을 아는 사람 있습니까? null 인 경우 쿼리 매개 변수를 추가하고 싶지 않습니다.

밀가루

Andreiyfrag의 대답은 내 문제를 해결하는 방법에 대한 아이디어를 얻었습니다. Action2의 뷰에서 다음과 같이 수동으로 routevaouedictionary를 생성합니다.

var searchArguments = new Dictionary<string, object>();
if (ViewBag.Foo != null)
{
    searchArguments.Add("foo", ViewBag.Foo);
}
if (ViewBag.Bar != null)
{
    searchArguments.Add("bar", ViewBag.Bar);
}

그리고 다음과 같이 사용하십시오.

@Url.Action("Action1", new RouteValueDictionary(searchArguments) )

하위 작업 ( Url.Action) 보기에서 URL을 구성 하면 제공된 모든 쿼리 매개 변수가 포함되지만 (null 인 경우에도) 동일한 작업이 일반 작업으로 실행 된 경우 (비 하위 작업) ), null 매개 변수가 생략되었습니다. 왜 이런 일이 발생하는지 모르겠습니다. Razor 또는 MVC의 버그 일 수 있습니까?

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관