0

我正在使用 MVC 4 和实体框架开发 Intranet Web 应用程序。从开发开始,我就有一个我无法解决的问题。为了说明我在说什么,这里有一个例子:

我可以通过“创建视图”添加一个人。在这个观点中,我必须精确一个十进制数,它代表这个人的房子和工作地点之间的距离。在这个阶段,一切都很好。但是,当我想编辑一个人并想保存更改时,该数字不被接受为十进制数。

我阅读了我的问题,并尝试了在 web.config 中添加标签和精确我想要使用的文化(准确地说是法国文化)。

这是我的“编辑视图”:

@model BuSIMaterial.Models.Person
@{
    ViewBag.Title = "Edit";
}
<h2>
    Edit</h2>
<script src="@Url.Content("~/Scripts/jQueryFixes.js")" type = "text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Person</legend>
        @Html.HiddenFor(model => model.Id_Person)
        <div class="editor-label">
            First name :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.FirstName, new { maxlength = 50 })
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
        <div class="editor-label">
            Last name :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { maxlength = 50 })
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        <div class="editor-label">
            National number :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.NumNat, new { maxlength = 11 })
            @Html.ValidationMessageFor(model => model.NumNat)
        </div>
        <div class="editor-label">
            Start date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker" })
            @Html.ValidationMessageFor(model => model.StartDate)
        </div>
        <div class="editor-label">
            End date :
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker" })
            @Html.ValidationMessageFor(model => model.EndDate)
        </div>
        <div class="editor-label">
            Distance House - Work (km) :
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.HouseToWorkKilometers)
            @Html.ValidationMessageFor(model => model.HouseToWorkKilometers)
        </div>
        <div class="editor-label">
            Category :
        </div>
        <div class="editor-field">
            @Html.DropDownList("Id_ProductPackageCategory", "Choose one ...")
            @Html.ValidationMessageFor(model => model.Id_ProductPackageCategory) <a href="../ProductPackageCategory/Create">
                Add a new category?</a>
        </div>
        <div class="editor-label">
            Upgrade? :
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Upgrade)
            @Html.ValidationMessageFor(model => model.Upgrade)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

你有解决这个问题的想法吗?这很烦人,不幸的是,我没有找到解决它的方法。

编辑:编辑方法(人控制器):

    public ActionResult Edit(long id = 0)
    {
        Person person = db.Persons.Single(p => p.Id_Person == id);
        if (person == null)
        {
            return HttpNotFound();
        }
        ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name", person.Id_ProductPackageCategory);
        return View(person);
    }

    //
    // POST: /Person/Edit/5

    [HttpPost]
    public ActionResult Edit(Person person)
    {

        ViewBag.Id_ProductPackageCategory = new SelectList(db.ProductPackageCategories, "Id_ProductPackageCategory", "Name", person.Id_ProductPackageCategory);

        if (ModelState.IsValid)
        {
            ModelStateDictionary errorDictionary = Validator.isValid(person);

            if (errorDictionary.Count > 0)
            {
                ModelState.Merge(errorDictionary);
                return View(person);
            }

            db.Persons.Attach(person);
            db.ObjectStateManager.ChangeObjectState(person, EntityState.Modified);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(person);
    }
4

1 回答 1

0

如果不显眼的 javascript 验证结果与服务器端的不同,您的问题可能与MVC 3 jQuery Validation/globalizing of number/decimal field相关。

我们有一个类似的问题,这个线程帮助解决了它。

于 2013-03-26T10:29:29.227 回答