2011-06-19 25 views
1

我正在使用asp.net mvc3。我在SQL Server中設計了數據庫。我使用ado.net訪問數據庫,我正在設計一個「CREATE」頁面。如何在創建新行時在字段中插入修復值

這是我view.cshtml

@model CalcoWOMS.Models.ProductFormulation 

@{ 
    ViewBag.Title = "doProductFormulation"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>ProductFormulation</legend> 

     <div> 


      @Html.EditorFor(model => model.ProductID) <!-- i want to insert fix value (100) in this ProductID field in ProductFormulation Table --> 
      @Html.ValidationMessageFor(model => model.ProductID) 
    </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.RawMaterialID) 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("RawMaterialID","SELECT") 
      @Html.ValidationMessageFor(model => model.RawMaterialID) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Code) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Code) 
      @Html.ValidationMessageFor(model => model.Code) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.MinimumBatchSize) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.MinimumBatchSize) 
      @Html.ValidationMessageFor(model => model.MinimumBatchSize) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Quantity) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Quantity) 
      @Html.ValidationMessageFor(model => model.Quantity) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "ProductFormulationIndex") 
</div> 

我想在這個領域ProductIDProductFormulation表中插入固定值(100),並且不希望創建可編輯框。我應該爲此做什麼 ?

+2

現在有多少個問題在過去幾天被問及mvc?先參考一些教程。嘗試首先制定解決方案 – Eranga

+0

如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」)以很好的格式和語法突出顯示它! –

+1

你也可以使用:htmlAttribute你設置的位置:<%= Html.Editor(「測試名稱」,「動作」,新的價值=「100」})%>也會這樣做 – cpoDesign

回答

1

也許我不明白的問題(我更後端的開發者),但它似乎是一個簡單的解決方法是刪除這兩條線:

<div> 
     @Html.EditorFor(model => model.ProductID) 
     @Html.ValidationMessageFor(model => model.ProductID) 
</div> 

然後更新您的控制器的ModelCreate,[post]方法將ProductID更新爲100,然後再將其寫入存儲庫。當然,你怎麼做取決於你沒有發佈的控制器結構。

據我瞭解,在替代方法是在頁面上隱藏字段,並在初始Create調用創建Model爲100 ProductID但如果你總是要硬編碼的產品ID(說實話似乎有問題)到100,那麼這似乎毫無意義...

+0

先生你是對的,我們應該對@ Html.EditorFor(model => model.ProductID)允許用於編寫ProductId,但我希望將100存儲在Table(模型)中,不需要從用戶那裏獲取輸入? –

+0

@Pushpendra:當你已經知道你正在編寫的值時,不應該有任何需要從用戶那裏獲得輸入。您應該可以在控制器中設置該值。 – forsvarir

+0

謝謝你,先生,非常感謝。我的問題解決了。先生,我可以知道你的電子郵件ID,所以我可以直接提出任何問題。如果你沒有任何問題,請告訴我你的電子郵件ID。我的電子郵件ID是[email protected] –

相關問題