我試圖生成TextArea
盒由控制器提供的默認值。鑑於這種標記它調用的局部視圖:MVC TextAreaFor與價值呈現空箱
@{
Html.RenderAction(
"ContentBlock", "ContentBlock",
new TestSiteMvc.Models.ContentModel() { ContentID = 3 }
);
}
管窺:
@Html.TextBoxFor(m=>m.Title)
@Model.ContentHtml<br />
@Html.TextAreaFor(m => m.ContentHtml)
@Html.TextBoxFor(m => m.ContentHtml)
@Html.TextArea("ContentHtml2", Model.ContentHtml)
@Model.ContentHtml<br />
渲染生成的HTML是:
<input id="Title" name="Title" type="text" value="Home Title" />
Home Page Content<br />
<textarea cols="20" id="ContentHtml" name="ContentHtml" rows="2"></textarea>
<input id="ContentHtml" name="ContentHtml" type="text" value="Home Page Content" />
<textarea cols="20" id="ContentHtml2" name="ContentHtml2" rows="2">Home Page Content</textarea>
Home Page Content<br />
所以,很明顯是越來越填充ContentHtml財產。我不明白的是爲什麼TextAreaFor
呈現爲一個空值。並且要清楚,當我刪除行@Html.TextBoxFor(m => m.ContentHtml)
,以不產生與同一ID標籤,這個問題不會消失。
控制器:
[HttpGet]
public ActionResult ContentBlock(Models.ContentModel mcontent) {
mcontent = new Models.ContentModel() {
ContentID = 3,
ContentHtml = "Home Page Content",
Title = "Home Title" };
return PartialView(mcontent);
}
型號:
public class ContentModel {
public int ContentID { get; set; }
public string Link { get; set; }
public string Title { get; set; }
public string ContentHtml { get; set; }
public DateTime LastModifiedDate { get; set; }
}
不能重複這一點。 –
您的控制器代碼在哪裏?使用給定的ViewModel屬性測試視圖代碼,它已經工作,您需要提供控制器操作方法來清除textarea默認值問題。 –
@TetsuyaYamamoto,我添加了控制器和一些額外的代碼。謝謝。 – InbetweenWeekends