简体   繁体   中英

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

I have a model Products like this,

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

Here is my view,

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

Here is my controller,

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

And here is my sql code,

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

I am getting "procedure or function ProductCreate has too many arguments specified" error.

What am I doing wrong?

When you call a stored procedure, dapper has no good way of knowing what members should be treated as parameters. With regular tsql, it can have a stab at "obviously not used", " obviously used", "maybe" - but dapper does not choose to pay the extra cost of asking the database what your stored procedure looks like all the time. I suspect it is adding properties that aren't used - ProductId in particular. Help it out by mapping to an anon type. Instead of passing model , pass:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM