我正在使用asp.net mvc3。我在SQL Server中设计了数据库。我使用ado.net访问数据库,我正在设计一个“创建”页面。
这是我的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>
我想在ProductID
表中的ProductFormulation
字段中插入固定值(100),并且不想创建可编辑框。我应该怎么
这样做?
答案 0 :(得分:1)
也许我不明白这个问题(我更像是一个后端开发人员),但似乎简单的解决方案就是删除这两行:
<div>
@Html.EditorFor(model => model.ProductID)
@Html.ValidationMessageFor(model => model.ProductID)
</div>
然后更新控制器的Model
,Create
方法中的[post]
,将ProductID
更新为100,然后再将其写入存储库。当然,你如何做到这一点取决于你没有发布的控制器结构。
根据我的理解,替代方法是在页面上设置一个隐藏字段,并在初始Model
调用中创建ProductID
,Create
为100,但如果你'总是要将产品ID(说实话似乎有问题)硬编码到100,那么这似乎毫无意义......