1

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?

5
  • 2
    string is already nullable. But you cannot have 2 nullable values as parameters and use routes (you will only be able to generate /Details?name=xxx - if you were to use /Details/xxx both parameters will be null and your code will always return BadRequest) Commented May 16, 2017 at 9:10
  • @StephenMuecke public ActionResult Details( int id = 0, string name = null) is return me a bad request....is there any way to solve this problem or i need to follow other structure Commented May 16, 2017 at 9:19
  • Just have one parameter (string) and then check if it can be parsed to int Commented May 16, 2017 at 9:21
  • @StephenMuecke i can understand can you just give me one little example? Commented May 16, 2017 at 9:23
  • Give me 20 min and I'll add an answer. Commented May 16, 2017 at 9:25

3 Answers 3

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

0

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

String is a reference type so it allows null by default

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.