如何在创建新行时在字段中插入修复值

时间:2011-06-19 14:07:02

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-3

我正在使用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),并且不想创建可编辑框。我应该怎么  这样做?

1 个答案:

答案 0 :(得分:1)

也许我不明白这个问题(我更像是一个后端开发人员),但似乎简单的解决方案就是删除这两行:

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

然后更新控制器的ModelCreate方法中的[post],将ProductID更新为100,然后再将其写入存储库。当然,你如何做到这一点取决于你没有发布的控制器结构。

根据我的理解,替代方法是在页面上设置一个隐藏字段,并在初始Model调用中创建ProductIDCreate为100,但如果你'总是要将产品ID(说实话似乎有问题)硬编码到100,那么这似乎毫无意义......