nayan chowdhury
nayan chowdhury

Reputation: 283

.net MVC Action with Optional Parameters

I want that controller will accept /Details/1 and /User/xxx but not /User/

and i try it like below=>

    public ActionResult Details( Nullable<int> id, Nullable<string> name)
    {
        if (((id ?? 0) == 0) && name == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        Instructor instructor = db.Instructors.Where(x => x.InstructorId == id || x.faculty_name.Equals(name)).SingleOrDefault();

        if (instructor == null)
        {
            return HttpNotFound();
        }

        ViewBag.faculty_active = MyCustomFunctions.UserActivity();
        return View(instructor);
    }

i want that(mansion above) user can pass /Details/1 or /Details/xxx but not /Details/ thats why i add condition check=>

        if (((id ?? 0) == 0) && name == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

but i wants to run the code the compiler gives me error something like=> enter image description here i was thinking that was perfect but i dont know why a have done wrong = i tried=>

    public ActionResult Details( int? id, string? name)
    {
    }

that was also gives me the same error.i searched about this problem and i found=> ASP.NET MVC Overriding Index action with optional parameter

i am not understanding anything . thats why i reposed this kind of problems. this will be great full if anybody can help me?

Upvotes: 1

Views: 3360

Answers (3)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48367

Don't use Nullable<string> because you do not needed and makes no sense.

string already accepts null value and that means is already nullable.

Short example:

string myString=null;

Upvotes: 2

user3559349
user3559349

Reputation:

string is already nullable so Nullable<string> and string? make no sense. However you cannot have 2 nullable parameters and achieve the routes you want. The routing engine has no way of knowing which of the parameters should be bound, and if you were to use ../Details/xxx both values would be null and you would always be returning HttpStatusCode.BadRequest.

You could achieve this using 2 separate methods (say DetailsByID and DetailsByName) and route definitions, but if you want a single method, then you can only have one parameter to account for all 3 routes. In the method you can then attempt to parse the value to int

public ActionResult Details(string id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    int number;
    Instructor instructor;
    bool isInt = Int32.TryParse(id, out number);
    if (IsInt)
    {
        instructor = db.Instructors.Where(x => x.InstructorId == number).SingleOrDefault();
    }
    else
    {
        instructor = db.Instructors.Where(x => x.faculty_name.Equals(id)).SingleOrDefault();
    }
    if (instructor == null)
    {
        return HttpNotFound();
    }
    ViewBag.faculty_active = MyCustomFunctions.UserActivity();
    return View(instructor);
}

Upvotes: 4

Sameer549
Sameer549

Reputation: 31

You need to add Nullable for Value Types, like int,demical,DateTime etc,.

String is a reference type so it allows null by default

Upvotes: 0

Related Questions