2015-10-13 34 views
0

我已經允許HTML模型上的屬性,像這樣:我得到一個HttpRequestValidationException甚至當我AllowHtml的Model屬性

型號

public class FooViewModel 
{ 
    [AllowHtml] 
    public string Description { get; set; } 
} 

查看

@using (Html.BeginForm("EditDescription", "Foo", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken(); 

    <input type="hidden" value="@(Model != null && Model.Item1 != null ? Model.Item1 : string.Empty)" name="fooId" /> 

    <p> 
     <textarea name="Description" cols="100" id="Description"> 
      @(Model != null && Model.Item2 != null ? Model.Item2 : string.Empty) 
     </textarea> 
    </p> 

    <p><input type="submit" value="Submit" /></p> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/nicEdit") 

    <script type="text/javascript"> 
     bkLib.onDomLoaded(function() 
     { 
      new nicEditor({fullPanel : true}).panelInstance('Description'); 
     }); 
    </script> 
    } 

控制器

public class FooController 
{ 
    public ActionResult EditDescription(string fooId) 
    { 
    if (Request.HttpMethod == "POST") 
    { 
     using (var context = new ApplicationDbContext()) 
     { 
     var foo = context.Foos 
        .SingleOrDefault(f => f.Id == fooId); 

     // I get the HttpRequestValidationException here 
     foo.Description = Request["Description"]; 
     context.SaveChanges(); 

     return RedirectToAction("MyProfile"); 
     } 
    } 
    } 
} 

不過,我得到一個請求驗證例外。我甚至試圖與ValidateInput(false)註釋整個動作的方法,因爲該圖只有一個領域,這是對模型的單一屬性,但我仍然不斷收到異常。

我清除了ASP.NET的臨時文件和文件夾的緩存,清理和重建我的解決辦法都無濟於事。

+0

你的基本代碼有很多基礎錯誤。你不應該使用Request.HttpMethod,而是應該使用[HttpPost和HTTPGET(http://stackoverflow.com/questions/5332275/httppost-vs-httpget-attributes-in-mvc-why-use-httppost) 。其次,你沒有使用綁定來發回模型,所以你的屬性幾乎沒用。 –

+0

@ErikPhilips我很關注他們。我一直在使用MVC自第1版。這種特殊的作用和它的整個流程我在趕時間,與我們會回來給它解決它的客戶端相互的協議後做了,現在是時候了。我在你的評論之後通過分離每種方法的操作並實際發回模型來修復它。我只是沒有意識到(因爲你推斷)請求驗證和每個HTTP請求沒有單獨的方法之間的關聯。 –

回答

相關問題