Kyle
Kyle

Reputation: 33701

Using Guid in link fails

I am building a MVC4 project with razor engine, although I don't know if that has relevance to my problem. I have a VendorBillController with a Submit(String poId) ActionResult. My link looks like this: example.com/VendorBill/Submit?poId=790a2e76-5186-42c1-8087-c13b18d22023

However, when trying to go to this page, I get this error:

The view '722884' or its master was not found or no view engine supports the searched locations.

Why is it looking for the view 722884? Although I was pretty sure numbers, letters, and dashes were OK in html links, I tried html encoding the link as well but nothing changes.

if I put in

example.com/VendorBill/Submit?poId=asdf

It makes it to the function, but then fails because the poId is passed into new Guid() and asdf is not a guid.

Why does a guid in a URL fail? Is there a 'normal' work around for this, when a guid is the ID that needs to be passed?

edit:

Here is the code for my entire controller:

It doesn't work with guid either, here is my entire controller, however I don't think it's even getting there so i'm not sure it's the issue:

    public class VendorBillsController : Controller
    {
        public ActionResult Submit(Guid poId)
        {

            var purchaseOrderId = new Models.Entities().TempLinks.AsQueryable().Single(x => x.Guid == poId).PurchaseOrderId;


            return View(purchaseOrderId.ToString());
        }
    }
}

Upvotes: 1

Views: 385

Answers (2)

Glenn Slaven
Glenn Slaven

Reputation: 34203

Does it work if you change the signature of Submit to Submit(Guid poId)?

There is no issue with using GUIDs natively in MVC it will convert them for you.

If the above doesn't work, can you please include the code of the controller method so we can have a look at it?

Update: No, your problem is this:

return View(purchaseOrderId.ToString());

If you pass a string into View, it assumes that what you're passing is the name of the view (see this on MSDN), if you want your view model to be of type string then you're going to have to pass the view name in as the first parameter, then the master view name, then the string value (this overload). Much easier to declare an explicit view model which has a single property PurchaseOrderId then pass that in to the view method. The way the View method overloads are setup you can use any view model type except string.

You could just pass it in if it was a GUID, but you have to pass it in as a GUID, don't call ToString()

Upvotes: 3

SBurris
SBurris

Reputation: 7448

The problem comes in when you want to pass only a string to a strongly typed view. What MVC is expecting when you pass in a string is the name of the view you want to load. This is why it was looking for and could not find a view based on the Guid string you passed in.

MSDN: Controller.View(string viewName)

Gleen Slaven is correct that if you change the type from a string to a Guid then you will not have this problem.

Upvotes: 1

Related Questions