문제

I have a partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DomainModel.Entities.Product>" %>

<div class="item">
    <h3><%= Model.Name %></h3>
    <%= Model.Description %>

    <% using (Html.BeginForm("AddToCart", "Cart")) { %>
        <%= Html.Hidden("ProductID") %>
        <%= Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery) %>
        <input type="submit" value="+ Add to cart" />
    <% } %>

    <h4><%= Model.Price.ToString("c")%></h4>
</div>

그리고 여기에는 html 을 렌더링:

<div class="item"> 
    <h3>Kayak</h3> 
    A boat for one person
    <form action="" method="post">
        <input id="ProductID" name="ProductID" type="hidden" value="1" /> 
        <input id="returnUrl" name="returnUrl" type="hidden" value="/" /> 
        <input type="submit" value="+ Add to cart" /> 
    </form> 
    <h4>$275.00</h4> 
</div> 

아무 일도 일어나면서 제출 버튼을 클릭하고 나는 그들이 합의하기 때문에 양식을 작업 속성 값이 없습니다.안 BeginForm(액션,컨트롤러)을 돌보는 렌더링의 양식을까요?무엇이 잘못된 것입니까?

편집

코드에서 CartController AddToCart 행동:

    public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
    {
        Product product = productsRepository.Products.FirstOrDefault(p => p.ProductID == productID);

        cart.AddItem(product, 1);
        return RedirectToAction("Index", new { returnUrl });
    }

편집 2

View 렌더링하는 부분적인:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% foreach (var product in Model) { %>
        <% Html.RenderPartial("ProductSummary", product); %>
    <% } %>

    <div class="pager">
    Page:
    <%=Html.PageLinks((int)ViewData["CurrentPage"],
                      (int)ViewData["TotalPages"],
                      x => Url.Action("List", new { page = x, category = ViewData["CurrentCategory"] })) %>
    </div>
</asp:Content>

3 편집

경로:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            null, // don't need a name
            "", // matches the root URL, i.e. ~/
            new { controller = "Products", action = "List", category = (string)null, page = 1 } //Defaults
        );

        routes.MapRoute(
            null, // don't need name
            "Page{page}", // URL pattern, e.g. ~/Page683
            new { controller = "Products", action = "List", category = (string)null }, // defaults
            new { page = @"\d+" } // constraints: page must be numerical
        );

        routes.MapRoute(null,
            "{category}",
            new { controller = "Products", action = "List", page = 1 });

        routes.MapRoute(null,
            "{category}/Page{page}",
            new { controller = "Products", action = "List" },
            new { page = @"\d+" } // constraints: page must be numerical
        );

    }
도움이 되었습니까?

해결책

그것처럼 보이지 않는 기본 경로를 설정합니다. BeginFormUrlHelper.GenerateUrl 일치하는 조치/컨트롤러 이름기있는 컬렉션입니다.그래서 만약 당신이하지 않는 경로가 있는 지도 AddToCart, 다음 그것을 할 수 없는 URL 을 생성한다.를 추가하려고 이를 아래의 경로:

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

다른 팁

이것은 주 응용 프로그램 예제에서 사용되는 스티븐 Sanderson 의 우수한'프로 ASP MVC 프레임워크'책입니다.

충분히 재미있게 만들었을 정확히 동일한 실수는 생략합니다.노선 통화에서 주어진 목록 페이지 130.

routes.MapRoute("Default", "controller}/{action}"

그것은 자니 G 의 대답은 이 게시물에 도움을 찾아 나의 실수로습니다.

좋은 하나 Johnny!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top