Asp.net MVC检查此注册是否免费 [英] Asp.net MVC check if this registration free or not

查看:186
本文介绍了Asp.net MVC检查此注册是否免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net mvc中的新手.Pleace帮助我检查是否存在我在数据库中编写的值.4列值应检查并返回消息

我尝试过的事情:

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; }
    }
}




我的注册级别(检查VaqonID& amp; PoezdID&& MestoID& amp; Date)是否存在.





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);
       }


这是registrationsController


this is registrationsController

推荐答案

您可以编写一个检查其存在的方法.例如:

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

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();
            }
}



然后,在您的Create动作中,您可以调用上述方法以检查是否存在:



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

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");
      }
}



如果您不熟悉ASP.NET MVC,建议您在这里参考我的系列文章:



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[^]


我认为你要求您检查在表单上输入的值,如果缺少某些值,它将显示一条消息.这里有一篇文章对此进行了描述:
ASP.NET MVC客户端验证 [如何:在数据模型中自定义数据字段验证 [
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


这篇关于Asp.net MVC检查此注册是否免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆