1

I'm new to asp.net mvc. I have index method with parameter id :

 public ActionResult Index(int id)
    {

        var dc = new ServicesDataContext();
        var query = (from m in dc.Mapings
                   where m.CustomerID == id
                    select m);
       // var a = dc.Customers.First(m => m.CustomerId == id);
       // ViewData.Model = a;
       // return View();
        return View(query);
    }

Now when I try to redirect to index from edit i get an error " The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MVCServices.Controllers.CustomerserviceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

   [HttpPost]
    public ActionResult Edit( FormCollection form)
    {
        var id = Int32.Parse(form["CustomerServiceMappingID"]);

        var datacontext = new ServicesDataContext();
        var serviceToUpdate = datacontext.Mapings.First(m => m.CustomerServiceMappingID == id);
        TryUpdateModel(serviceToUpdate, new string[] { "CustomerID", "ServiceID", "Status" }, form.ToValueProvider());

        if (ModelState.IsValid)
        {
            try
            {
                var qw = (from m in datacontext.Mapings
                          where id == m.CustomerServiceMappingID
                          select m.CustomerID).First();
                datacontext.SubmitChanges();
                //return Redirect("/Customerservice/Index/qw");
                return RedirectToAction("Index", new { qw = qw });
            }
            catch{
                }
        }

        return View(serviceToUpdate);
    }

This is the View:

                 @Html.ActionLink("Back to List", "Index")

The id in the Index method turns out to be the customerid fetched from another controller while id in Edit is from another table.Can you please let me know the mistake I've been doing and how to solve it?

tereško
  • 58,060
  • 25
  • 98
  • 150
Sayamima
  • 205
  • 1
  • 8
  • 24

1 Answers1

4

Do this in the Edit action:

return RedirectToAction("Index", new { id = qw });
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Hey . One more thing is that I have a link to go back in cshtml i.e. the view ... SO, can u just let me know how I can use the same varibale from controller in view – Sayamima Jun 09 '11 at 03:37
  • Which variable are you trying to pass to the view? And which action method? – Andrew Cooper Jun 09 '11 at 03:39
  • the same qw value should go into view – Sayamima Jun 09 '11 at 03:41
  • Just add the value to the ViewData collection in the controller for the view and then do `@Html.ActionLink("Back to List", "Index", new { id = ViewData["qw"])` in the view. – Andrew Cooper Jun 09 '11 at 04:40