2012-01-04 3 views
0

문제가 생겼습니다. 각 행에 대한 양식이있는 webgrid가 필요합니다.이 작업을 수행했지만 제출 버튼을 클릭하면 컨트롤러가 입력 텍스트를받지 못합니다. MVC WebGrid 내의 양식

뷰에 대한 코드입니다 :

@grid.GetHtml(
    tableStyle: "mGrid", 
    headerStyle: "head", 
    alternatingRowStyle: "alt", 
    rowStyle: "altRow", 

    columns: grid.Columns(
       grid.Column(columnName: "Id", header: "Id", style: "prefix"), 
       grid.Column(columnName: "Trademark", header: "Marca", style: "trademark"), 
       grid.Column(columnName: "Price", header: "Precio", style: "price", format: @<text>@item.Price.ToString("N2")</text>), 
       grid.Column(format: (item) => 
           { 
            System.Text.StringBuilder html = new System.Text.StringBuilder(); 

            html.Append("<form action=\"/Cart/AddToCart\" method=\"get\">"); 
            html.Append("<input type=\"text\" value=\"\" style=\"width:50px; text-align:center; \" name=\"quantity\" id=\"quantity\" />"); 
            html.Append("<input type=\"submit\" value=\"Agregar\" class=\"btnAdd\" />"); 
            html.Append("<input type=\"hidden\" name=\"productId\" value=\"" + item.Value.Id + "\"/>"); 
            html.Append("<input type=\"hidden\" name=\"returnUrl\" value=\"" + Request.Url + "\"/>"); 
            html.Append("</form>"); 

            return new HtmlString(html.ToString()); 
           } 
          ) 
      ) 
    ) 

그리고 이것은 컨트롤러의 일부입니다

public class CartController : Controller 
{ 
    private IDataRepository repository; 

    ... 

    public RedirectToRouteResult AddToCart(Cart cart, int productId, int quantity, string returnURL) 
    { 
     Product product = repository.Products.FirstOrDefault(p => p.Id == productId); 

     if (product != null) 
      cart.AddItem(product, quantity); 

     return RedirectToAction("Index", new { returnURL }); 
    } 

모든 것이 좋아 컴파일합니다. 하지만 실행 수량이 항상 null 인 경우, 나는 int와 string을 같은 결과로 이미 시도했습니다.

도움이 될 것입니다. 감사합니다. .

답변

1

시작하려면 URL 인수와 관련 숨김 필드를 삭제할 수 있습니다. Request.Referrer를 사용하여 대신 이전 작업으로 돌아가십시오.

초, 수량은 널이 될 수 없으며 0 또는 0이 아니지만 정수는 널이 될 수 없습니다.

세 번째, 장바구니는 어디서 오는가? 이것이 도메인 객체라면 액션에 ID를 전달하고 액션에서 장바구니를로드합니다. 나는 또한 문맥 특유의 DTO에 primatives를 결합 할 것이다. 난 정수 오류가 발생 양으로 사용하는 문자열로 수량을 시도 할 때 AddToCartCommand 두 번째 포인트에 대해,

class AddToCartCommand 
{ 
    public int CartId {get;set;} 
    public int ProductId {get;set;} 
    public int Quantity {get;set;} 
} 
+0

하이이다

AddToCart(AddToCartCommand input) { var cart = repository.Carts.First(input.CartId); var product = repository.Products.First(input.ProductId); cart.Add(product, input.Quantity); return RedirectToAction(Request.Referrer); } 

은 널이다. 셋째, 장바구니는 바인드 된 객체이므로 정상적으로 작동합니다. 그리고 추천자 어쩌면 UrlReferrer? 시간 내 줘서 고마워. – Gabriel

+0

컨트롤러 개체 외부에서 도메인 개체를로드하지 않습니다. 시도 할 다음 것은 명시 적 type = "text"및 value = ""을 제거하는 것입니다. –

+0

고맙습니다 !!!! 이제 해결되었습니다 ... 나는 value = ""를 제거하고 훌륭하게 작동합니다. 그것은 너무 작았 다 ... :) – Gabriel