0

my webpage takes list from customer in the same time i want to display all records from temp_data. PROBLEM is i have to give same id's for table so that i can get the data based on the ID. So please tell something like Ispostback (webforms) in MVC

I WANT TO RETAIN id variable to give same id but it changes on every post. Please help me

{

    RMS_DataContext db = new RMS_DataContext();
    Order cls_order = new Order();
    string id = Guid.NewGuid().ToString();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection form, string btn_orders)
        { 

            switch (btn_orders)
            {       
                case "btn_temp":
                    Item item = new Item();

                    var ItemId = form["_OrderId1"];
                    item = db.Items.FirstOrDefault( x=> x.ItemId==ItemId );
                    cls_order.Add_Temp(id, item, Convert.ToInt32(form["quantity"].ToString())); 

                    return View();
                    break;

                    default:
                    return View();
            }
        }

}

1 Answers1

1

You create a separate action in your controller to handle the POST action, which would essentially be a "Postback" from webforms. You would then handle any items in the POST which you would have handled in your Postback.

If you're handling validation, be sure to check ModelState.IsValid and return your model to your View again which should then display validate errors

public ActionResult Edit()
{
   var model = new EditModel() { Id=Guid.NewGuid() };
   return View(model);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditModel model) 
{
   if (ModelState.IsValid) {
      // do stuff when valid
      return RedirectToAction("Index"); //or whatever when you're done saving, etc...
   }

   return View(model);
}

In your View:

@Html.HiddenFor(x=>x.Id);

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
  • My case is of ADD , i generate GUID in string ID_ than in every insert i want to save the same ID_ so that i can get all data of this ID_ , string ID_= Guid.NewGuid().ToString(); .... so when there is post it generates new id..... I want ISpostback liketheing to RETAIN this GUID – ASP.Net Developer Oct 14 '14 at 16:04
  • then use a Guid instead of int, and use that as your id, it'll always post with that in the url – Anthony Shaw Oct 14 '14 at 16:09
  • just saw your edit... the Controller is not static, so Id will always contain a new Guid each and every time. You will need to either assign it on your model and hold it in a hidden field and post it along with your other data, or use my suggestion above – Anthony Shaw Oct 14 '14 at 16:10
  • Can you just write some code so that i can get your idea. – ASP.Net Developer Oct 14 '14 at 16:12
  • I updated based on your edit, stubbed code, losely based on your posted code – Anthony Shaw Oct 14 '14 at 16:13
  • u can see that in my CREATE VIEW i have not binded any model because in my case there are several models to be used.... so how can i return a model in my view – ASP.Net Developer Oct 14 '14 at 16:19