1

This BankId field is required. When call the controller & execute that code always return false. BankID data value entry one by one automatic & BankName data insert user input wise.

please any suggestion how to solve that problem in asp.net mvc razor How to solve this problem. This BankId field is required. When call the controller & execute that code always return false. BankID data value entry one by one automatic & BankName data insert user input wise

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

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Md. Azizul Hoque
  • 181
  • 1
  • 1
  • 6
  • you can check your buttons for validaton by giving 'same name' to all the buttons and then passing that name to the controller action as 'string buttonname' like this http://stackoverflow.com/questions/1714028/mvc-which-submit-button-has-been-pressed – maztt Jan 07 '16 at 10:37

2 Answers2

0

Int are not nullable thus making it always required without you putting data annotation. You could change your BankId to public int? BankId { get; set; }

Bon Macalindong
  • 1,310
  • 13
  • 20
0

Basically you should create different viewmodels for Create and Edit first that will solve your problem. you will not be having BankId in your Create viewmodel. that how the thing should go in asp.net mvc.

but in your scenario you could remove bankid from modelstate when creating the form before hitting model.IsValid

if (FrmCollection["Save"] != null)
{
       ModelState.Remove("BankId");
}       
maztt
  • 12,278
  • 21
  • 78
  • 153