Title: Problems about return different View
Tags: asp.net-core;.net-core
Question: Here is the code in controller:
[Route(&quot;{page:required}.html&quot;)]
public IActionResult Index(string page)
int.TryParse(page, out int i);
if (i!= 0)
{
//Return View A
}
else
{
//Return View B
}
public IActionResult A(string i)
//some logic
return View(AModel);
public IActionResult B(int i)
//some logic
return View(BModel);
As the code above, I wanna achieve this:
When the page is an int(but not zero), then return the B view.
When the page is a string, then return the A view.
AModel and the BModel is different.
I have tried some ways:
For example, return RedirectToAction will change the URL.
Return View will throws an error:The model item passed into the ViewDataDictionary is of type 'System.Int32', but this ViewDataDictionary instance requires a model item of type 'Sample.Database.BModel'.
What should I do to achieve this without change the URL?Thank you.

Here is the accepted answer: Update
If you want to use a different view for each action it might help stating it explicitly:
public IActionResult A(string i)
//some logic
return View(&quot;A&quot;, AModel);
public IActionResult B(int i)
//some logic
return View(&quot;B&quot;, BModel);
The problem may be in the rest of your code, not shown here, but normally this should work:
[Route(&quot;{page:required}.html&quot;)]
public IActionResult Index(string page)
int.TryParse(page, out int i);
if (i!= 0)
return B(i);
else
return A(page);
public IActionResult A(string i)
//some logic
return View(&quot;A&quot;, AModel);
public IActionResult B(int i)
//some logic
return View(&quot;B&quot;, BModel);
There is some inconsistency in what you are saying
When the page is an int(but not zero), then return the A view.
public IActionResult A(string i)
Comment for this answer: Then it's something caused by the code not shown here; you should add the rest of your code.
Comment for this answer: @MelonNG - check the update in the beginning of my post.
Comment for this answer: @MelonNG - as you are returning it in `Index(string page)` it will normally use the `Index` view; specifying it in your A method will overwrite that and use the `A` view.
Comment for this answer: No, it reports the same error:The model item passed into the ViewDataDictionary is of type 'System.Int32', but this ViewDataDictionary instance requires a model item of type 'SealIndustry.Database.BModel'.
Comment for this answer: I am sorry about the inconsistency, I have corrected it just now.
Comment for this answer: Well, it works now. Would you please tell me what is the difference between return View("A", AModel); and return View(AModel);


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:11/18/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net-core><.net-core>', 'Title': 'Problems about return different View', 'ViewCount': 35, 'id': 64889537, 'user_AboutMe': '', 'user_AccountId': 10440421.0, 'user_DisplayName': 'Melon NG', 'user_Reputation': 2174.0, 'user_Views': 384.0, 'user_id': 7697261.0}}