Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have developed the material request slip in mvc.. when i save the items to grid the header textboxes are cleared. how to retain the values. please help me... Below showdata function give static value its works: for eg. i give "vivek".

public ActionResult ShowData(int page = 1, string sort = "Id", string sortDir = "ASC")
           const int pageSize = 10;
           bool Dir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? true : false;
           var maxId = (db.MRHdr.Select(x => (int?)x.Id).Max() ?? 0) + 1;
           var irc = "MRN-" + maxId;

           MRHdr mrhdr = new MRHdr() { MRNo = irc, MRDate = DateTime.Now, Remarks="Vivek"};
           var student = mobjModel.GetMRDPage(page, pageSize, sort, Dir, mrhdr.MRNo);
           var totalRows = mobjModel.CountStudent(mrhdr.MRNo);

           var data = new PagedStudentModel()
               TotalRows = totalRows,
               PageSize = pageSize,
               VwMatReq = student,
             //  MRDtl = student,
               MRHdr = mrhdr
           return View(data);

What I have tried:

$(".save").live("click", function () {
       var id = $("#grid tbody tr").length;
      // var itemname = $("#Item_Name_" + id).val();
       var ircode = $("#ItemR_Code_" + id).val();
       var icode = $("#Item_Code_" + id).val();
       var qtyreq = $("#Qty_Req_" + id).val();
       var mrno = $("#MRNo").val();

       if (id != "") {
               type: "GET",
               contentType: "application/json; charset=utf-8",
               url: '@Url.Action("SaveRecord", "MRD")',
               data: { "ircode": ircode, "icode": icode, "qtyreq": qtyreq, "mrno": mrno },    //"itemname": itemname,
               dataType: "json",
               beforeSend: function () { },
               success: function (data) {
                   if (data.result == true) {
                     //  $("#divmsg").html("Record has been saved successfully !!");
                     //  setTimeout(function () { window.location.replace("ShowData"); }, 2000);
                   else {
                       alert('There is some error');

      public JsonResult SaveRecord(string itemname, string ircode, string icode, int qtyreq, string mrno)
          bool result = false;
              result = mobjModel.SaveMRDtl(ircode, icode, qtyreq, mrno);
          catch (Exception ex)
          return Json(new { result }, JsonRequestBehavior.AllowGet);  
Updated 20-Nov-16 19:16pm
Vivek.anand34 19-Nov-16 2:36am
I have updated my question. above show data function I gave Remarks="Vivek" its executed. but i need what i enter in remarks textbox..
Can't you send the textbox value to ShowData?

In ASP.NET MVC retaining data is very simple;
if you POST a form where controller found some error and revert back the view with input module bound with it, so no worry.

If you do a custom Ajax post, HTML would not be changed, you need to show the Action result on top of the earlier HTML, simple.

If you do it from inline grid editing, following article may help;

Grid inline editing demo
Vivek.anand34 21-Nov-16 1:01am
3 days am trying not resolve this problem.. please anyone help...
am save using javascript and [HTTPGET] in controller.
Vivek.anand34 21-Nov-16 1:05am
Actually in script window.location.replace("ShowData"); without this line. save but didn't bind in grid. with this line binded. but header textbox cleared.
anup.bhunia 21-Nov-16 1:49am
Not very clear what you mean by header may post some screenshot.
In a separate note, you should not use HTTPGET method for saving data.
Vivek.anand34 21-Nov-16 4:16am
How to attach file here.. and see above its my save code..
You already have all the data in client side. Just make a html table row and append that to the table.

I don't think you need to call the "ShowData" method.

You can use something like below to append a row to table.
$('#myTable > tbody:last-child').append('<tr><td>Data 1</td><td>Data 2</td></tr>');
Vivek.anand34 21-Nov-16 2:33am
How to append it.. can u sample code..
Check my updated answer.
Vivek.anand34 21-Nov-16 3:16am
actually the grid values comes from database.. when i click the save button in grid it saved and show in grid.
Then just return those values from the save method itself and append inside success method using those values.
Vivek.anand34 21-Nov-16 4:22am
above code is my save method code.. how to get values in success method.
You have already returned the result. Just read it in success method.
Vivek.anand34 21-Nov-16 4:46am
sorry, I am new to mvc can u send sample code how to read it..
There is no point of mvc here. You can do like below.

success: function (data) {
var someValue = data.PropertyName;
Vivek.anand34 21-Nov-16 5:09am
here propertyName means below 'mrno' right.
var mrno = $("#MRNo").val();
No that is the property of the object you returned from the Save method. As you said you wanted those values, so I told you like that.

So, currently the method "mobjModel.SaveMRDtl" is returning true/false. As you need all the values, so try returning those values which you need in the client side to append to the table.
Vivek.anand34 21-Nov-16 6:08am
Ok I will try..
Vivek.anand34 21-Nov-16 5:10am
and were i declare that 'someValue'.
Vivek.anand34 21-Nov-16 6:17am
can we bind data from db without append...
No. This is the best way.
Vivek.anand34 21-Nov-16 7:32am
This is my grid. how to append in this grid:
htmlAttributes: new { id = "grid" },
fillEmptyRows: false,
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("Id", header: "ID", canSort: true),
grid.Column(header: "Item Name",format: @ @item.ItemName @Html.TextBox("Item_Name_" + (int)item.Id, (string)item.ItemName, new {@class="ItemName", @style = "display:none" })),
grid.Column(format: @ @item.IRateCode @Html.TextBox("ItemR_Code_" + (int)item.Id, (string)item.IRateCode, new { @class = "IRCode", @style = "display:none" })),
grid.Column(header: "Item Code",format: @ @item.ItemCode @Html.TextBox("Item_Code_" + (int)item.Id, (string)item.ItemCode, new { @class = "ItemCode", @style = "display:none" })),
grid.Column(header: "Unit Price",format: @ @item.UnitPrice @Html.TextBox("Unit_Price_" + (int)item.Id, (int)item.UnitPrice, new { @class = "UnitPrice", @style = "display:none" })),
grid.Column(header: "Qty Requested",format: @ @item.QtyRequested @Html.TextBox("Qty_Req_" + (int)item.Id, (decimal)item.QtyRequested, new { @style = "display:none" })),
// grid.Column(header: "MRNo",format: @ @item.MRNo @Html.TextBox("MR_No_" + (int)item.Id, (string)item.MRNo, new { @style = "display:none" })),
grid.Column(header: "Action",format:@<text> Edit
As I said, everything on browser is rendered as plain html. So, the grid is rendered as a table. I have already told you how to append a row to a table. Check that on my answer.
Vivek.anand34 21-Nov-16 8:21am
i will give my source and db can you see... my screen
No sorry. You have to do that. I have helped with codes as well. At least you can try. You are not even trying.
Vivek.anand34 21-Nov-16 23:38pm
append means dynamically store values in grid right.. but i need it from db. suppose i want to update means i will update based on Id..
I told you to return those database values after you save. So, inside save method, the call to save method should return you all the necessary details you need for the grid row. Then inside success method, you can get those values. Now for a table row using those values and append that to the table.
Vivek.anand34 22-Nov-16 3:19am
see above comment grid coding that is row. i want to append in that row.. but there is no row id.. how to declare in that grid..
Vivek.anand34 22-Nov-16 3:58am
$(document).ready(function () {
$.getJSON(url1, json,
function (json) {
var tr;
for (var i = 0; i < json.length; i++) {
tr = $('');
tr.append("" + json[i].id + "");
tr.append("" + json[i].itemname + "");
tr.append("" + json[i].itemcode + "");
tr.append("" + json[i].ircode + "");
tr.append("" + json[i].qtyreq + "");

am using this code to append but didn't append.. any error in this code.
It should be...

tr = $('tr');

If it does not work, try debugging.
Vivek.anand34 22-Nov-16 4:17am
I use in this site it executed:

there accepted answer click jsfiddle..
Please debug and check in developer console.
Vivek.anand34 22-Nov-16 4:20am
and there what is json, and how i give in url..

i have given like this
var json = { "id": id, "itemname": itemname, "ircode": ircode, "icode": icode, "qtyreq": qtyreq };
var url1 = '@Url.Action("GetGrid", "MRD")';

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900