简体   繁体   中英

Using textbox to enter value into database table Asp.net MVC

I have a list of items that I pull from a database and at the same time I want the user to enter the quantity issued.

My problem is that after I click the button print/issue the amount in the database for quantity issued is 0.

I have added a TextBox for the user to enter the value. Not sure if what I have here is correctly done.

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

View

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

Controller

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

I can't see anywhere in your controller that you have updated or selected from the model the quantity. I See pulling data from the database, I see where your pushing to the database, but I don't see anywhere your setting the quantity.

Normally you'll have your view (with a begin form)

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

Then in your controller you'd have something to read the model data your passing back. There are three main ways to do this,

1) Using a public FormCollection

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

2) Using direct named values

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

3) Using a model view

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

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