我正在使用MVC 4和實體框架在Intranet Web應用程序上工作。自發展開始以來,我有一個我無法解決的問題。爲了說明我在說什麼,這裏是一個例子:MVC 4文化衝突
我可以通過「創建視圖」添加一個人。在這個觀點中,我必須精確地表示一個十進制數字,它代表了這個人的房子和工作地點之間的距離。在這個階段,一切都很好。但是,當我想編輯人員並想要保存更改時,該號碼不被接受爲十進制數字。
我讀到了關於我的問題,我嘗試了諸如在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);
}
你會得到什麼樣的錯誤?或者它只是在小數位切斷或只是轉換爲int? – ChrFin 2013-03-26 09:52:23
Javascript的驗證結果不同於服務器端的驗證結果嗎?我們對小數點分隔符有類似的問題,但只有服務器端驗證按預期工作。 – Peit 2013-03-26 09:52:33
你可以發佈你的Action方法的代碼嗎? – 2013-03-26 09:53:56