我在理解@Razor
如何在視圖中工作時遇到一些麻煩。下面的代碼是我的觀點,用戶可以創建一個新的帖子(我正在創建一個論壇)不正確的剃刀語法
我想要做的是刪除<Fieldset>
我的問題是,我不能改變我的代碼標記。
@model Forum3.Models.Posts
<h2>CreatePost</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
//--- CAN'T EDITED ----
<fieldset>
<legend>Post</legend>
@*SET TopicID*@
@{
Html.HiddenFor(model => model.TopicId);
@Html.Hidden("TopicId",ViewData["currentTopicId"]);
}
//----END----
<div class="editor-label">
@Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
如果我刪除<Fieldset
>和<Legend
>我得到這個錯誤我HiddenFor
代碼: Parser Error Message: Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.
如果我再刪除@{...}
看起來像這樣:
@Html.HiddenFor(model => model.TopicId);
@Html.Hidden("TopicId",ViewData["currentTopicId"]);
我當我點擊Create
時會發生錯誤,因爲TopicId沒有設置爲我的currentTopicId
(如果我離開,也會發生這種情況)
我不知道這裏發生了什麼。有任何想法嗎?
謝謝,那就是問題!我沒有完全弄清楚如何讓模型包含'TopicId',所以我只保留ViewData。 – NoClueBlue 2013-04-25 17:52:43