0

ビューにエラーメッセージを表示するのを手伝ってください。私のコードは以下のとおりです-

これは私のビューモデルです:

public class DepartmentViewModel
{
    //public Department Department { get; set; }
    public string DepartmentId { get; set; }
    [Required]
    public string DepartmentName { get; set; }
    public string ManagerMemberId { get; set; }
    public bool IsActive { get; set; }
    public List<Member> Managers { get; set; } 
}

これはコントローラーです:

public class DepartmentController : Controller
{
    //
    // GET: /Department/

    public ActionResult Index()
    {
        var adminUserId = (string)Session["UserId"];
        var memberId = (string)Session["MemberId"];

        List<Department> departments = null;

        if (!string.IsNullOrEmpty(adminUserId))
        {
            departments = new DepartmentManager().GetDepartments(false);
        }
        else if (!string.IsNullOrEmpty(memberId))
        {
            var companyId = (int)Session["CompanyId"];
            departments = new DepartmentManager().GetDepartments(false, companyId);
        }

        //var managers = new MemberManager().GetMembersOfManagerCategory(true, );

        return View("DepartmentList", departments);
    }


    public ActionResult Delete(string departmentId)
    {
        try
        {
            new DepartmentManager().DeleteDepartment(departmentId);
        }
        catch (System.Exception exception)
        {
            ModelState.AddModelError("Error", "Unable to delete department. Try again,      and if the problem persists see your system administrator.");
            //return RedirectToAction("Index", "Department");
        }

        return RedirectToAction("Index", "Department");
    }
}

これはビューです:

@model IEnumerable<PDCA.Core.EntityClasses.Department>
@{
    ViewBag.Title = "Department List";
    Layout = "~/Views/Shared/_Layout.cshtml";
 }
<div class="container">
@using (Html.BeginForm())
{
    <form class="form-horizontal">
    <fieldset>
        <legend>List of Departments:</legend>
        <table border="1" cellspacing="1">
            <tr>
                <th>
                    Id
                </th>
                <th>
                    Name
                </th>
                <th>
                    Company
                </th>
                <th>
                    Manager
                </th>
                <th>
                    IsActive
                </th>
                <th>
                </th>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.DepartmentId)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DepartmentName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Company.CompanyName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Manager.MemberName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.IsActive)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { item.DepartmentId }) |
                        @Html.ActionLink("Delete", "Delete", new { item.DepartmentId })
                    </td>
                </tr>
            }
        </table>
        <p></p>
        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
        <p></p>
        <p>
            @Html.ValidationMessage("Error");
        </p>
    </fieldset>
    </form>
}

ビューに @Html.ValidationMessage("Error") を入れましたが、エラーメッセージは表示されません。

4

3 に答える 3

1

リダイレクトしているため、モデル状態はなくなりました。tempdata を介して保存できますが、私はそのようにはしません ( RedirectToAction を使用するときに ModelState エラーを維持するにはどうすればよいですか? )。特別なフィールドといくつかの通知メッセージ (toastr など) を使用してエラーを表示するか、単に同じビューに留まるとモデル状態エラーが表示されます。

public ActionResult Delete(string departmentId)
    {
        try
        {
            new DepartmentManager().DeleteDepartment(departmentId);
        }
        catch (System.Exception exception)
        {
            ModelState.AddModelError("Error", "Unable to delete department. Try again,      and if the problem persists see your system administrator.");
            //return RedirectToAction("Index", "Department");
YOU SHOULD ADD MESSAGE TO VIEWDATA HERE, OR RETURN SAME VIEW(make delete POST action also)

        }
    return RedirectToAction("Index", "Department");
}
于 2013-03-12T18:52:52.067 に答える
1

まず、フォームが重複しています

@using (Html.BeginForm())
{
    <form class="form-horizontal">

@Html.BeginFormformタグを処理します。

@Html.ValidationSummary次に、そのように追加してみてください

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <fieldset>

第 3 に、正しいスクリプト ファイルを参照する必要があります。通常、これはレイアウト ページで行われますが、それはあなた次第です。

への参照を追加

jquery
jquery.validate.js
jquery.validate.unobtrusive.j

CDN はここで見つけることができます

を入力しないと仮定してDepartmentName、送信を押すと、エラー メッセージが表示されます。

ページの は でなければならないことに注意しmodelてくださいDepartmentViewModel。上記の例ではそうではありません。

于 2013-03-12T18:48:58.193 に答える
0

@glosrob のように提案されているように、検証の概要を追加してみてください

@using (Html.BeginForm()) 
{
   @Html.ValidationSummary()
...

ValidationMessageForのようにも使用します

<td>
     @Html.DisplayFor(modelItem => item.DepartmentName)
     @Html.ValidationMessageFor(v=>v.DepartmentName)
 </td>
于 2013-03-12T19:01:30.650 に答える