简体   繁体   English

如何使用ASP.NET MVC剃须刀中的实体框架检查数据库中是否存在记录?

[英]How to Check if record exist in database using Entity Framework in ASP.NET MVC razor?

Is it possible for a controller to check if record exists in database using ADO.NET? 控制器是否可以使用ADO.NET检查数据库中是否存在记录?

Here is my controller 这是我的控制器

[HttpPost]
public ActionResult Add(TemporaryVoucher temporaryVoucher)
{
    string fileName = Path.GetFileNameWithoutExtension(temporaryVoucher.ImageFile.FileName);
    string extension = Path.GetExtension(temporaryVoucher.ImageFile.FileName);
    fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;

    /*temporaryVoucher.VoucherPath = "/Image/" + fileName;
    fileName = Path.Combine(Server.MapPath("/Image/"), fileName);*/
    temporaryVoucher.VoucherPath = fileName;

    using (DBModels db = new DBModels())
    {
        db.TemporaryVouchers.Add(temporaryVoucher);
        db.SaveChanges();
    }

    ModelState.Clear();
    return View();
}

Here is my ado.net in model 这是我的ado.net模型

public partial class TemporaryVoucher
{
    public int PromoKey { get; set; }
    public string PromoName { get; set; }
    [DisplayName("Upload Image")]
    public string VoucherPath { get; set; }

    public HttpPostedFileBase ImageFile { get; set; }
}

Here is my view 这是我的看法

<div class="form-group">
        @*Html.LabelFor(model => model.ImageName)*@
        <label name="PromoName" style="text-align: right; clear: both; float:left;margin-right:15px;">PromoName</label>
        <div class="col-md-10">
            @*Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control", required = "required" } })
                @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })*@
            <input type="text" name="PromoName" id="txtInput" onkeypress="return checkSpcialChar(event)" required />
        </div>
    </div>
    <div class="form-group">
        @*Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })*@
        <label name="ImagePath" style="text-align: right; clear: both; float:left;margin-right:15px;margin-top:5px;">Upload Image</label>
        <div class="col-md-10">
            <input type="file" style="margin-top:5px;" name="ImageFile" accept=".jpg,.jpeg,.png" required />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Upload" style="margin-top:5px;" class="btn btn-default" />
        </div>
    </div>
</div>

I just want to ensure no possible duplication for PromoName in the database 我只想确保数据库中PromoName不可能重复

You can perform a 'get' within your controller action based on any identity column to check for an existing value in the database. 您可以根据任何标识列在控制器操作中执行“获取”,以检查数据库中的现有值。 If present, return with a message, else add the incoming model to the database. 如果存在,则返回一条消息,否则将传入的模型添加到数据库中。 Something like this: 像这样:


using (DBModels db = new DBModels())
{   
    TemporaryVoucher tv = (from t1 in db.TemporaryVouchers
                           where t1.PromoKey == temporaryVoucher.PromoKey  // any identifier comparison can be done here
                           select t1).FirstOrDefault();
    if(tv != null)
        // return with a message that incoming temporary voucher already exists
    db.TemporaryVouchers.Add(temporaryVoucher);
    db.SaveChanges();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 和 ASP.NET MVC:如何检查数据库中是否存在记录并返回 id - C# and ASP.NET MVC : how to check if record exist in database and return id 如何在没有实体框架的 asp.net Mvc 中使用 CheckBox 删除多条记录 - How Can Delete Multiple Record using CheckBox In asp.net Mvc without Entity Framework 如何使用ASP.NET MVC中的Entity Framework将记录插入到具有外键的表中 - How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC 如何使用实体框架检查 ASP.NET MVC 中的删除操作是否成功 - How to check if a delete operation succeeds in ASP.NET MVC using Entity Framework 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? ASP.NET MVC - 如何首先使用实体框架模型/数据库部署到 Azure? - ASP.NET MVC - How to Deploy to Azure using Entity Framework Model/Database First? 如何在asp.net mvc中使用下拉列表在数据库中插入记录? - How to Insert a record in database using dropdownlist In asp.net mvc? 如果使用实体框架(DB 优先)(ASP.NET MVC)的记录中已经存在唯一字段组合,则阻止发布 object - Prevent posting of object if combination of unique fields already exist in a record with Entity Framework (DB First) (ASP.NET MVC) 未创建ASP.NET MVC实体框架数据库 - ASP.NET MVC Entity Framework Database not being created PopUp 不在 ASP.NET MVC 5 和实体框架中的数据库中发送数据 - PopUp not sending data in database in ASP.NET MVC 5 and Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM