skip to Main Content

I want to call an action in the controller in the view and I have to send a series of values ​​to the controller, but the action calls the controller but sends null values. please help me

enter image description here

Code in view:

var _getDocumentsUrl = '@Html.Raw(@Url.Action("GetDocuments", "Document", new { Section = "0", Province = "1", County = "0", District = "0", area = "Province" }))';

Action in Controller:

public IActionResult GetDocuments(AreaVM area)
{///codes////}

Model:

public class AreaVM
{
    public string? Section {  get; set; }
    public string? Province {  get; set; }
    public string? County {  get; set; }
    public string? District {  get; set; }
}

Sending the values ​​of section = 0, Province = 1, County = 0, and District = 0 to the controller

2

Answers


  1. Chosen as BEST ANSWER

    My problem was solved by changing area to model in GetDocuments action

    public IActionResult GetDocuments(AreaVM model)
    

  2. You have to specify from what part of query your object should be constructed. Url.Action pass parameters to uri, so probably you need that ([FromUri] added):

    public IActionResult GetDocuments([FromUri]AreaVM area)
    

    Read this for more details:
    https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api#using-fromuri

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search