Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC中使用实体框架检查数据库中是否存在记录?_C#_Asp.net Mvc_Razor_Ado.net - Fatal编程技术网

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

C# 如何在ASP.NET MVC中使用实体框架检查数据库中是否存在记录?,c#,asp.net-mvc,razor,ado.net,C#,Asp.net Mvc,Razor,Ado.net,控制器是否可以使用ADO.NET检查数据库中是否存在记录 这是我的控制器 [HttpPost] public ActionResult Add(TemporaryVoucher temporaryVoucher) { string fileName = Path.GetFileNameWithoutExtension(temporaryVoucher.ImageFile.FileName); string extension = Path.GetExtension(tempora

控制器是否可以使用ADO.NET检查数据库中是否存在记录

这是我的控制器

[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();
}
这是我的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; }
}
这是我的看法

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

@*LabelFor(model=>model.ImageName)*@
促销名称
@*EditorFor(model=>model.ImageName,new{htmlAttributes=new{@class=“form control”,required=“required”})
@Html.ValidationMessageFor(model=>model.ImageName,“,new{@class=“text danger”})*@
@*LabelFor(model=>model.ImagePath,htmlAttributes:new{@class=“controllabel col-md-2”})*@
上传图像

我只想确保PromName在数据库中没有可能的重复

您可以基于任何标识列在控制器内执行“获取”操作,以检查数据库中的现有值。如果存在,则返回消息,否则将传入模型添加到数据库中。大概是这样的:


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


您判断重复的标准是什么?因此,您的
临时凭证
部分类
,另一部分是什么?您可以使用LINQ<代码>数据库临时凭证.FirstOrDefault。你可以读到它