Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new in asp.net mvc.Pleace help me to check if exist the value which I wrote in my database.4columns value should check and return message

What I have tried:

namespace poezd.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Reqistration
    {
        public int ID { get; set; }
        public string CustomerName { get; set; }
        public Nullable<int> PoezdID { get; set; }
        public Nullable<int> VaqonID { get; set; }
        public Nullable<int> MestoID { get; set; }
        public Nullable<System.DateTime> Date { get; set; }
    
        public virtual Poezd Poezd { get; set; }
        public virtual MestoVaqon MestoVaqon { get; set; }
        public virtual Vaqon Vaqon { get; set; }
    }
}



my reqistration class(check if VaqonID && PoezdID && MestoID && Date)have exist.


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Reqistration</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PoezdID, "PoezdID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("PoezdID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PoezdID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.VaqonID, "VaqonID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("VaqonID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.VaqonID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.MestoID, "MestoID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("MestoID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MestoID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @Html.
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}


[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Create([Bind(Include = "ID,CustomerName,PoezdID,VaqonID,MestoID,Tarix")] Reqistration reqistration)
       {
           if (ModelState.IsValid )
           {
               db.Reqistration.Add(reqistration);
               db.SaveChanges();
               return RedirectToAction("Index");
           }


           ViewBag.PoezdID = new SelectList(db.Poezd, "ID", "Name", reqistration.PoezdID);
           ViewBag.MestoID = new SelectList(db.MestoVaqon, "ID", "ID", reqistration.MestoID);
           ViewBag.VaqonID = new SelectList(db.Vaqon, "ID", "Vaqon1", reqistration.VaqonID);
           return View(reqistration);
       }

this is registrationsController
Posted
Updated 3-Jul-18 17:37pm

You could write a Method that checks for their existence. For example:

C#
public bool IsEntryExist(Reqistration reqistration) {
            using (DBEntities db = new DBEntities()) {
                return db.Reqistration.Where(o => o.VaqonID.Equals(reqistration.VaqonID)
			|| o.PoezdID.Equals(reqistration.PoezdID)
			|| o.MestoID.Equals(reqistration.MestoID)).Any();
            }
}


Then in your Create action, you can then call the method above to check for existence:

C#
if (ModelState.IsValid )
{
      if(IsEntryExist(reqistration)){
		ModelState.AddModelError("", "VaqonID or PoezdID or MestoID already exist.");
      }
      else{
      		db.Reqistration.Add(reqistration);
      		db.SaveChanges();
      		return RedirectToAction("Index");
      }
}


If you are new to ASP.NET MVC, I'd recommend you to refer my series of article here: ASP.NET MVC 5: Building Your First Web Application - Part 1[^]
 
Share this answer
 
v2
I think you're asking that the values entered on the form have to be checked and it should display a message if some values are missing. There's an article that describes that here:
ASP.NET MVC Client Side Validation[^]

Some background information that I've used can be found here
How to: Customize Data Field Validation in the Data Model[^]
The part about using a partial class for validation metadata comes in handy when you're using edmx because when you update the data model it doesn't wipe out your metadata classes.

If you're asking about reading from the database and checking that they exist, well that's another topic.

HTH, Mike
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900