其實很簡單但是有時會無法繫結
Controller部份先
//抓最大值 int Sort = (from u in db.TabContent where u.TabID==tabid select u.Sort).DefaultIfEmpty().Max(x => x == null ? 0 : x); ViewBag.Sort = Sort + 1;以上這部份很簡單沒問題吧
程式碼自動產生會這樣@Html.LabelFor(model => model.Sort, new { @class = "control-label col-md-2" })@Html.TextBoxFor(model => model.Sort) @Html.ValidationMessageFor(model => model.Sort)
但如何修改成能抓ViewBag
改成用弱型別是一個方法記得"Sort"這樣就能抓到ViewBag了@Html.LabelFor(model => model.Sort, new { @class = "control-label col-md-2" })@Html.TextBox("Sort",null, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Sort)
而且最好以欄位名稱命名
這樣在Model Binding時就能抓到值喔
不然會抓不到資料有興趣可以試試 參考網址