2011-11-28 126 views
23

此html是否有任何問題?我想在主頁面中有一個鏈接以導航到「CreateParts」視圖。我有控制器'PartList'中具有參數parentPartId的'CreateParts'動作。將參數傳遞給來自Html.ActionLink的控制器動作

<li id="taskAdminPartCreate" runat="server"> 
            <%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 })%></li> 

我的控制器動作就像是

public ActionResult CreateParts(int parentPartId) 
    { 
     HSPartList objHSPart = new HSPartList(); 
     objHSPart.Id = parentPartId; 
     return View(objHSPart); 
    } 

當我在SITEMASTER的菜單中點擊「創建新零件」,我得到異常。請幫我解決這個問題。

+2

將異常添加到問題將是很好:) –

+13

老兄,什麼是地獄是runat =服務器在那個可憐的李上做?不要強姦MVC :) – rouen

+0

魯恩,你可以幫助最佳做法在asp.net mvc –

回答

55

您正在使用不正確的過載。您應該使用此重載

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 

和正確的代碼將

<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 }, null)%> 

需要注意的是,在年底額外的參數。 對於其他過載,請訪問LinkExtensions.ActionLink Method。正如你所看到的,你正在嘗試使用沒有string, string, string, object重載。

+1

你很棒@archill。你節省了很多時間,謝謝 –

+0

謝謝,我失蹤了。 –

10

您正在使用不正確的ActionLink重載。試試這個

<%= Html.ActionLink("Create New Part", "CreateParts", "PartList", new { parentPartId = 0 }, null)%> 
9

加成接受的答案:

,如果你打算使用

@Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 },null) 

這將創建ActionLink的,你不能爲該鏈接創建新的自定義屬性或樣式。

但是,ActionLink擴展中的第4個參數將解決該問題。以您的方式使用第四個參數進行自定義。

@Html.ActionLink("LinkName", "ActionName", "ControllerName", new { @id = idValue, @secondParam= = 2 }, new { @class = "btn btn-info", @target = "_blank" }) 
相關問題