簡體   English   中英

如何在MVC中插入數據並使用Dapper返回插入的標識?

[英]How to insert data and return inserted identity with Dapper in MVC?

我有這樣的模型產品,

public int ProductID { get; set; }
public int ProductDetailsID { get; set; }
public int ProductStatusID { get; set; }
public string ProductName { get; set; }
public int Priority { get; set; }
public DateTime LastProcessedDate { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
public bool Enabled { get; set; }

這是我的看法,

@model MVCApplication1.Models.Products
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(i => i.ProductDetailsID, new { @Value = ViewBag.ProductDetailsID })
    <div class="form-group">
        @Html.LabelFor(i => i.Name, "Product Name ", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(i => i.Name, new { @class = "form-control" })
            @Html.ValidationMessageFor(i => i.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(i => i.Enabled, "Enabled ", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(i => i.Enabled, new { @class = "checkbox", @checked = "checked" })
            @Html.ValidationMessageFor(i => i.Enabled)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

這是我的控制器,

[HttpPost]
public ActionResult Create(Products model)
{
   try
   {
       if (ModelState.IsValid)
       {
           model.LastProcessedDate = DateTime.Now;
           model.CreatedDate = DateTime.Now;
           model.ModifiedDate = DateTime.Now;
           model.ProductStatusID = 0;
           int id = con.Query<int>("ProductInsert", model, commandType: CommandType.StoredProcedure).Single();

           return Content("Product added successfully..!!", "text/plain");
            }
        }
        catch (Exception)
        {
            throw;
        }
        return View();
 }

這是我的sql代碼,

CREATE PROCEDURE [dbo].[ProductCreate]
(
  @ProductDetailsId INTEGER,
  @ProductStatusID INTEGER,
  @ProductName VARCHAR(255),
  @Priority INTEGER,
  @LastProcessedDate DATETIME,
  @CreatedDate DATETIME,
  @ModifiedDate DATETIME,
  @Enabled BIT
)
AS
INSERT INTO dbo.Products (ProductDetailsId, ProductStatusId, Product, Priority, LastProcessedDate, CreatedDate, ModifiedDate, Enabled, ScheduleTypeId, Server, RetryNumber, Message) 
VALUES(@ProductDetailsId, @ProductStatusId, @ProductName, @Priority, @LastProcessedDate, @CreatedDate, @ModifiedDate, @Enabled)

SELECT SCOPE_IDENTITY() AS ProductID

我得到“過程或函數ProductCreate指定了太多參數”錯誤。

我究竟做錯了什么?

當您調用存儲過程時,dapper無法知道哪些成員應被視為參數。 對於常規的tsql,它可能會“明顯不使用”,“明顯使用”,“可能” - 但是dapper並沒有選擇支付額外的費用來詢問數據庫您的存儲過程一直是什么樣子。 我懷疑它正在添加未使用的屬性 - 特別是ProductId 通過映射到anon類型來幫助它。 傳遞:而不是傳遞model

new { model.ProductDetailsId, model.ProductStatusID, ...todo... ,
    model.Enabled }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM