4

Pretty new to MVC5 but gaining ground quickly. This small issue has me stumped and there does not seem to be much information on Goolge (or I am not asking Google the right questions).

I have a Table (FILE_RCPTS_LOG) This table has multi keys (2)
First Key is Field: TRACK_NMBR (int) Second Key is Field: TRANS_DT (date)

When I created my Controller, the default views were also created. And for the most part, they work fine. However, I am getting HttpNotFound Errors, when attempting to use the Edit\Delete\Details links

@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }
@Html.ActionLink("Details", "Details", new {/* id=item.PrimaryKey */ }) 

This would be fine, if my table only had one key. But how do I pass both keys?

The few solutions I found online seemed way to complicated for such a simple action. I think I am missing something obvious here . . .

This is the code for my controller (Details)

  public async Task<ActionResult> Details(int? id, DateTime id2)
    {

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        FILE_RCPTS_LOG fILE_RCPTS_LOG = await db.FILE_RCPTS_LOG.FindAsync(id);
        if (fILE_RCPTS_LOG == null)
        {
            return HttpNotFound();
        }
        return View(fILE_RCPTS_LOG);
    }

I have tried several ways of passing multiple keys, but nothing is working. I have read a few tutorials about using SPLIT but I could not get that working either. This seems like such a simple and very common thing, as I would think most tables have multi-keys.

Is there a simple way of doing this that I am not seeing? I feel like I am over-thinking it.

thanks!!!

ExecChef
  • 387
  • 2
  • 13

2 Answers2

1

You can follow this. The first, in ActionLink, pass all 2 key values in as a comma delimited string. For example in Detail action:

@Html.ActionLink("Details", "Details", new {id= @item.TRACK_NMBR +','+@item.TRANS_DT});

In Details ActionController, you need to parse each of the 2 keys out.

string[] splitid = id.Split(',');
            FILE_RCPTS_LOG obj = db.FILE_RCPTS_LOG.Find(Convert.ToInt32(splitid[0]),Convert.ToDateTime(splitid[1]));

Or you can transfer 2 params as your solution:

@Html.ActionLink("Details", "Details", new {id= @item.TRACK_NMBR, id2 =@item.TRANS_DT});

public async Task<ActionResult> Details(int? id, DateTime id2)
{
FILE_RCPTS_LOG obj = db.FILE_RCPTS_LOG.Find(id, id2);
}

Remember the order of the keys is important. Hope to help, my friend!

Tomato32
  • 2,145
  • 1
  • 10
  • 10
  • Hi Tomato32! Thanks for your reply. I went ahead and modified the ActionLink as you described. At that point I am getting an immediate error that I can not use Operator '+' with Int and and DateTime. – ExecChef Aug 01 '18 at 14:01
  • @ExecChef. Tr: id= @item.TRACK_NMBR.ToString() +','+@item.TRANS_DT.ToString() – Tomato32 Aug 01 '18 at 14:03
  • Perfect! One last thing. Now, back in my controller . . . I am receiving a different error after adding the parse. The error: 'int?' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'int' could be found (are you missing assembly or reference?). Do I need to add a reference to use Split? OR is there an error in my class . . . public async Task Details(int?) – ExecChef Aug 01 '18 at 14:29
  • @ExecChef: Change Details(string id). – Tomato32 Aug 01 '18 at 14:31
  • Thanks Tomato32. Almost There. At this point, I am getting a 'A potentially dangerous Request.Path value was detected from the client (:). ' Error. This is the contents of the url string: http://Base/Details/62%2c10/1/2013%2012%3a00%3a00%20AM. The parameters it should be passing are Track_Nmbr: 62 Trans_Dt: 2013-10-01 – ExecChef Aug 01 '18 at 14:48
  • @ExecChef: Refer the new solution below, my friend. It's easier :)) – Tomato32 Aug 01 '18 at 14:53
  • Thank you again. The new solution worked. It was simple and easy to implement. It really helped that your answers were detailed and useful. Thank you for your help.. – ExecChef Aug 01 '18 at 18:20
  • @ExecChef: Yeah! You're great, my friend :)) – Tomato32 Aug 01 '18 at 18:36
1

Assuming that you are passing the view a model, just return the model in the action link. then you will have everything you need to know, including both Primary keys.

Ibrahim Malluf
  • 657
  • 4
  • 6
  • @Ibraham Malluf: Thank you for responding. I think your answer would be beneficial for future use, and I will learn how to do this, as it seems simple enough. Thank you – ExecChef Aug 01 '18 at 18:23