dotnet / dotnet/aspnetcore

Model binder in Razor View

Open
#47,480 5 comments 0 reactions 0 assignees View on GitHub
area-mvc investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

I have a Model here

```
[Serializable]
public class StaffInfoModel
{
[Display(Name = "Work Number")]
public string workNum { get; set; }
}
```

Then In my Index view, I have a input tag to bind `workNum` property

```
@model StaffInfoModel
@{
ViewData["Title"] = "Home Page";
}




@{
var data = Model;
}






```

Here is my controller code:

```
public IActionResult Index()
{
return View();
}

public IActionResult Query()
{
return View();
}

[HttpPost]
public IActionResult Query(QueryInfoModel model)
{
StaffInfoModel staff = new StaffInfoModel();
staff.workNum = "1010101010";

Console.WriteLine(staff.workNum);

return View("Index", staff);
}

public IActionResult Privacy()
{
StaffInfoModel staff = new StaffInfoModel();
staff.workNum = "1010101010";

return View("Index", staff);
}
```

And here is my query page:

```
@model QueryInfoModel
@{
ViewData["Title"] = "Query Page";
}












```

The problem is when i access privacy, It will return index view with data, And the data can be show successfully. Then when I access query page and click button, the code will access query post action then return index view with data, But the data will not show in index page. Only i add `value="@Model.workNum"` manually in input tag can show the data normally, I don't know why the same code cause different situation.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.