0

Is it possible to separate the ID in a URL (Home/Index/10) from a form submit that uses HttpPost and pass them to a controller?

Further explaination

A code example could be:

[HttpPost]
public ActionResult Index(int id, CustomerInfo info)
{
    /*
     * Code
     */

    Return View();
}

CustomerInfo is an object which in this case would contain an int called "ID" and other customer related info.

If I submit a form and want to pass a CustomerInfo ID and a url parameter id both the url id and CustomerInfo.id will be the CustomerInfo id which I passed from the form. If I don't pass a CustomerInfo ID they both will be the url parameter id.

Simply looking at the url id in the controller isn't an option as I need to check if an ID is given in CustomerInfo or not.

I understand that I can just give the CustomerInfo ID another name (eg. CustomerInfoID) but would like to know if I can keep the url parameter id and the CustomerInfo ID the same names.

enter image description here

In the image above you'll see my issue. I did NOT provide an ID for CustomerInfo.ID. It simply passed it to there from the url parameter id when I submit my form. I want the ID of CustomerInfo to be empty when I don't provide one.

ADyson
  • 57,178
  • 14
  • 51
  • 63
HappyGuyDK
  • 29
  • 8
  • I'm not really sure what you mean. Of course you can set different values for the `id` and `info.ID` fields if you want to. It's entirely up to you when you submit the form, although I'm not sure how logical it is. Or have I missed the point? The explanation is a bit garbled TBH. Perhaps a concrete example, plus showing the form you use to submit this info, would be useful. – ADyson Oct 24 '17 at 13:19
  • I've always been bad at explaining myself. I've updated the post with a picture of my specific issue. – HappyGuyDK Oct 24 '17 at 13:27
  • 1
    But the customer Id is in the object, why do you want it twice? – Liam Oct 24 '17 at 13:29
  • I didn't find that the CustomerId had any importance in this question as it simply is an "id reference" so to say to the Customer which the current CustomerInfo belongs to. Every customer can have multiple info sections. So basically ID and CustomerId is not the same thing and CustomerId should not be taken into account. – HappyGuyDK Oct 24 '17 at 13:33
  • 1
    It sounds like you should either be populating `CustomerID` maybe using a `Html.HiddenFor` or you want a view model that has both ids in it – Liam Oct 24 '17 at 13:33
  • "I want the ID of CustomerInfo to be empty when I don't provide one.". But why? What's the difference between `id` and that value? They represent something different? What's the intended purpose of this action method? It would also help if you show the form which is being used to submit - presumably it's set up in such a way that it creates this situation for you. – ADyson Oct 24 '17 at 13:33
  • You want a [viewmodel](https://stackoverflow.com/questions/9326450/in-mvc-what-is-a-viewmodel) – Liam Oct 24 '17 at 13:34
  • There is no reason that Id in Customer object will be populated if it is not on the form. Add your view which contains your form, maybe we can help then. – Ziv Weissman Oct 24 '17 at 13:47

1 Answers1

1

If you want to send both things you need to abstract it out by building a view-model thus:

public class ViewModel()
{
   public CustomerInfo info {get; set;}
   public int id {get; set;}
}

Send this to your view instead of CustomerInfo

[HttpGet]
public ActionResult Index()
{
   ViewModel vm = new ViewModel()
   {
      info = info,
      id = 0
   }

   return View(vm);
   //changed from below:
   //return View(info);
}

Anything in your view that references CustomerInfo does this via the view-model now, for example:

@Html.TextBoxFor(t => t.InfoText)

is now

@Html.TextBoxFor(t => t.info.InfoText)
...

Accept it in your post argument:

[HttpPost]
public ActionResult Index(ViewModel viewModel)
{

    /*
     * Code
     */
    int id = viewModel.id;
    CustomerInfo info = viewModel.info;
    Return View();
}
Liam
  • 27,717
  • 28
  • 128
  • 190