ModelState始终返回false

时间:2016-01-07 07:34:29

标签: asp.net-mvc razorengine

此BankId字段是必需的。当打电话给控制器&执行该代码总是返回false。 BankID数据值逐个输入自动& BankName数据插入用户输入。

请在asp.net mvc razor中解决该问题的任何建议 如何解决这个问题呢。此BankId字段是必需的。     当打电话给控制器&执行该代码总是返回false。     BankID数据值逐个输入自动& BankName数据插入用户输入

Model Class:  that section declare table filed 
namespace gl.db
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;

    public partial class Bank
    {
        public Bank()
        {
            this.Branches = new HashSet<Branch>();
        }

        public int BankId { get; set; }n

        [Required(ErrorMessage = "*")]
        [Display(Name = "Bank")]
        public string BankName { get; set; }

        public virtual ICollection<Branch> Branches { get; set; }
    }
}


 Controller Code: declare how to insert into sql table


 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult frmbank_create(FormCollection FrmCollection, [Bind(Include = "BankId, BankName")] Bank bank)
        { 
            @ViewBag.Title = "Bank Information"; 

            int i = -1;

            if (FrmCollection["Refresh"] != null)
            {
                FrmClear();
                return View();
            }

            else
            {
                if (ModelState.IsValid)
                {
                    if (FrmCollection["Save"] != null)
                    {
                        i = objDBBank.Insert(bank);
                    }
                    else if (FrmCollection["Update"] != null)
                    {
                        i = objDBBank.Update(bank);
                    }

                    if (i > 0)
                    {
                        if (FrmCollection["Save"] != null)
                        {
                            ViewBag.Operation = "Insert";
                            ViewBag.Flag = "true";
                        }
                        else
                        {
                            ViewBag.Operation = "Update";
                            ViewBag.Flag = "true";
                        }

                        ModelState.Clear();
                        return View();
                    }
                    else
                    {
                        if (FrmCollection["Save"] != null)
                        {
                            ViewBag.Operation = "Insert";
                            ViewBag.Flag = "false";
                        }
                        else
                        {
                            ViewBag.Operation = "Update";
                            ViewBag.Flag = "false";
                        }
                    }
                }
            } 

            return View(bank);
        }



View Code : declare css file view in browser 

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

                        <div class="panel-body">
                            @Html.ValidationSummary(true)
                            <div class="row">
                                <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                    <div class="form-inner12">
                                        <div class="left-col">Bank</div>
                                        <div class="right-col"> 
                                            @Html.HiddenFor(model => model.BankId)
                                            @Html.TextBoxFor(model => model.BankName, new { @class = "form-control in-ctrl" })
                                            @Html.ValidationMessageFor(model => model.BankName)
                                        </div>
                                        <div class="clr"></div>
                                    </div>
                                </div>
                            </div>

                            <div class="clr20"></div>

                            <div class="row">
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                    <div class="form-inner12">
                                        <div class="left-col">&nbsp;</div>
                                        <button type="submit" class="btn btn-primary @ViewBag.BtnBankSaveDis" name="Save" value="Save">Save</button>
                                        <button type="submit" class="btn btn-primary @ViewBag.BtnBankUpdateDis" name="Update" value="Update">Update</button>
                                        <button type="submit" class="btn btn-primary" name="Refresh" value="Refresh">Refresh</button>
                                        <div class="clr"></div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    }

enter image description here

2 个答案:

答案 0 :(得分:0)

Int不可为空,因此在不放置数据注释的情况下始终需要它。您可以将BankId更改为public int? BankId { get; set; }

答案 1 :(得分:0)

基本上,您应首先为CreateEdit创建不同的视图模型以解决您的问题。您将不会在Create viewmodel中拥有BankId。那个东西应该怎么去asp.net mvc。

但在您的方案中,您可以在创建表单之前从modelstate中删除bankid,然后再点击model.IsValid

if (FrmCollection["Save"] != null)
{
       ModelState.Remove("BankId");
}