使用文本框在数据库表Asp.net MVC中输入值

时间:2017-12-29 20:11:29

标签: c# asp.net asp.net-mvc

我有一个从数据库中提取的项目列表,同时我希望用户输入已发布的数量。

我的问题是,单击按钮打印/发出后,数据库中发出的数量为0。

我添加了一个TextBox供用户输入值。不确定我这里的内容是否正确完成。

@Html.TextBoxFor(model => model.item.quantityI, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.item.quantityI, "", new { @class = "text-danger" })

查看

if (@Model.items.Count > 0)
{
    foreach (var issueditem in @Model.items)
    {
        <tr>

            <td class="col-md-4">@issueditem.itemNumber</td>
            <td class="col-md-4">@issueditem.description</td>
            <td class="col-md-4">@issueditem.expense_account.getDescription</td>
            <td class="col-md-2">@issueditem.quantity.ToString()</td>
            <td class="col-md-5">
                @*@Html.LabelFor(model => model.item.quantityI, htmlAttributes: new { @class = "col-md-3" })*@
                @Html.TextBoxFor(model => model.item.quantityI, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.item.quantityI, "", new { @class = "text-danger" })
            </td>
            <td class="col-md-1">@issueditem.selecteduomtext </td>
            <td class="col-md-1">@issueditem.price.ToString()</td>  
        </tr>
    }
}

控制器

public ActionResult ReceiptPrint(Issue issue)
{
    IssueDAO dbdata = new IssueDAO();
    dbdata.connectionString = ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString;
    getIssue.transactionDate = DateTime.Now; //Sets the transaction date to current date
    getIssue.status = -1;

    getIssue.docType = "Issue";
    ViewBag.StoresReps =dbdata.SelectEmployeesByDept("Stores");
    getIssue.employeeDetails.employeeNum = issue.employeeDetails.employeeNum;
    getIssue.processedbyDetails.employeeNum = issue.processedbyDetails.employeeNum;

    getIssue.inventory_acccount=5520;

    Item item = new Item();

    try
    {
        dbdata.createIssue(getIssue, item);//Creates the issue in the database
    }
    catch (Exception ex)
    {
        LogWrite logWriter = new LogWrite(ex.ToString());
        ViewBag.errorMessage = "Unable to complete the Issue. Please see Log file for more Information";
        return View("IssueItem", getIssue);

    }
    DataSet ds = dbdata.GetReceipt(getIssue.requisitionNumber);

    LocalReport localreport = new LocalReport();
    localreport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\Reciept.rdlc";
    localreport.DataSources.Add(new ReportDataSource("Receipt_Data", ds.Tables[0]));
    localreport.SetParameters(new ReportParameter("Req_num", getIssue.requisitionNumber));

    string reporttype = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension = "pdf";
    string deviceInfo = @"<DeviceInfo>              
             <OutputFormat>PDF</OutputFormat>              
             <PageWidth>8.5in</PageWidth>              
             <PageHeight>11in</PageHeight>          
             <MarginTop>0.25in</MarginTop>          
             <MarginLeft>0.45in</MarginLeft>            
             <MarginRight>0.45in</MarginRight>       
             <MarginBottom>0.25in</MarginBottom></DeviceInfo>";
    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;
    renderedBytes = localreport.Render(
     reporttype, deviceInfo, out mimeType, out encoding, out fileNameExtension,
     out streams, out warnings);


    var doc = new iTextSharp.text.Document();
    var reader = new PdfReader(renderedBytes);
    using (FileStream fs = new FileStream(Server.MapPath("~/Receipt" +
         Convert.ToString(Session["CurrentUserName"]) + ".pdf"), FileMode.Create))
    {
        PdfStamper stamper = new PdfStamper(reader, fs);
        string Printer = "Xerox Phaser 3635MFP PCL6";
        // This is the script for automatically printing the pdf in acrobat viewer
        stamper.JavaScript = "var pp = getPrintParams();pp.interactive =pp.constants.interactionLevel.automatic; pp.printerName = " +
                       Printer + ";print(pp);\r";
        stamper.Close();
    }
    reader.Close();
    FileStream fss = new FileStream(Server.MapPath("~/Receipt.pdf"), FileMode.Open);
    byte[] bytes = new byte[fss.Length];
    fss.Read(bytes, 0, Convert.ToInt32(fss.Length));
    fss.Close();
    System.IO.File.Delete(Server.MapPath("~/Receipt.pdf"));
    return File(bytes, "application/pdf", "receipt.pdf");
}

1 个答案:

答案 0 :(得分:0)

我无法在控制器中看到您已更新或从模型中选择数量的任何内容。我看到从数据库中提取数据,我看到你推送到数据库的位置,但我没有看到你设置数量的任何地方。

通常你会看到你的观点(带有开始表格)

@using (Html.BeginForm("ActionMethodName","ControllerName"))
{
     @Html.TextBoxFor(model => model.item.quantityI, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.item.quantityI, "", new { @class = "text-danger" })
     <input class="button" id="submit" type="submit" value="Submit" />
}

然后在您的控制器中,您可以阅读传回的模型数据。有三种主要方法可以做到这一点,

1)使用公共FormCollection

ActionResult ActionMethodName(FormCollection collection)   
{
    string quantity = collection.Get("quantityI);
}

2)使用直接命名值

ActionResult ActionMethodNAme(string quantityI)
{
     // The quantityI is already stored in the string parameter
}

3)使用模型视图

 ActionResult ActionMethodNAme(MyClass myItem)
{
     // MyClass would be defined as a class containing all your variables pulled from the form so you could do. You need to make MyClass in advance! 
     string quantity = myItem.quantityI;
}