2017-02-24 39 views
0

圖路線MVC 5條航線後的模型不能正常工作

routes.MapRoute(
    name: "ContactForm", 
    url: "contact-form.html", 
    defaults: new { controller = "SiteHome", action = "ContactForm" }, 
    namespaces: new[] { "Presentation.Controllers" }     
); 

操作方法

[AllowAnonymous] 
[AcceptVerbs(HttpVerbs.Post)] 
[ValidateAntiForgeryToken] 
public ActionResult ContactForm(ContactForm postForm) 
{ 
} 

聯繫表格代碼

@using (Html.BeginForm("IletisimForm", "SiteAnasayfa", FormMethod.Post, htmlAttributes: new { @class = "" })) 
{ 

    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

    @Html.TextBoxFor(model => model.Fullname, new { @class = "w-input easing" }) 
    @Html.ValidationMessageFor(model => model.Fullname, "", new { @class = "text-danger text-bold" }) 

    @Html.TextBoxFor(model => model.Telephone, new { @class = "w-input easing") 
    @Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger text-bold" }) 

    @Html.TextBoxFor(model => model.Email, new { @class = "w-input easing") 
    @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger text-bold" }) 

    @Html.TextAreaFor(model => model.Message, new { @class = "w-input easing" }) 
    @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger text-bold" }) 

    <input type="submit" class="fr w-button easing" value="Send" /> 

} 

當我發表我的形式..我刪除AntiForgeryToken( )並測試了相同的結果。

HTTP錯誤404.0 - 找不到

+0

ContactForm操作所在控制器的名稱是什麼? – MartinM

+0

控制器:SiteHomeController操作:ContactForm。獲取方法的作品,但當我嘗試顯示404後的帖子。 – Actionsee

+0

你是如何發佈表單?你是否正在呼叫處理防僞令牌客戶端? – MartinM

回答

0

你的ActionResult名 「的ContactForm」,但你張貼

@using (Html.BeginForm("IletisimForm", "SiteAnasayfa", FormMethod.Post, htmlAttributes: new { @class = "" })) 

它必須是;

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, htmlAttributes: new { @class = "" })) 

和你的路由一樣;

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

而你的動作就像;

[HttpPost] 
public ActionResult ContactForm(ContactForm postForm) 
{ 
} 

並刪除AntiForgeryToken,如果您發佈您的數據,您可以稍後添加。

+0

謝謝,但我換了英文。我使用相同的控制器名稱和操作名稱。我想使用特殊的網址。我知道它在默認路線上有效。 – Actionsee