dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.15k stars 9.92k forks source link

Model binder in Razor View #47480

Open sxr1999 opened 1 year ago

sxr1999 commented 1 year ago

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";
}
<div class="text-center">
    <form asp-controller="Home" asp-action="InputInfo" method="post" class="form-horizontal col-md-offset-4 col-md-8">

        @{
            var data = Model;
        }
        <div class="form-group form-inline">
            <label asp-for="workNum" class="control-label col-sm-4"></label>
            <input type="text" asp-for="workNum" class="form-control col-sm-4" readonly="readonly"/>
        </div>
    </form>
</div>

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";
}
<div class="text-center">
    <form asp-controller="Home" asp-action="Query" method="post" class="form-horizontal col-md-offset-4 col-md-8">
        <div class="form-group form-inline">
            <label asp-for="WorkNum" class="control-label col-sm-4"></label>
            <input type="text" asp-for="WorkNum" class="form-control col-sm-4" readonly="readonly" />
        </div>
        <span asp-validation-for="WorkNum" class="text-danger  col-sm-offset-3"></span>

        <div class="form-group form-inline">
            <label class="control-label col-sm-4"></label>
            <input type="submit" value="query" class="btn btn-primary col-sm-4" />
        </div>

    </form>
</div>

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.

ghost commented 1 year ago

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

sxr1999 commented 1 year ago

above code is just a minimalistic repro project. You can just copy it and run in your project.

javiercn commented 1 year ago

@sxr1999 thanks for the details.

Unfortunately, that's not enough for us to proceed. When we ask for a minimal repro project as a public GitHub repository it is because we do not think that we have enough details with the information provided.

There are many details that are subtle that you might think are not relevant but that make a difference, so in order to maximize our chances of helping you, we need all the detail possible, and the best way to provide that is with a project and steps to follow.

Otherwise, it's not possible for us to assist in this area.

sxr1999 commented 1 year ago

Here is the link about my public GitHub repository

ghost commented 1 year ago

To learn more about what this message means, what to expect next, and how this issue will be handled you can read our Triage Process document. We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. Because it's not immediately obvious what is causing this behavior, we would like to keep this around to collect more feedback, which can later help us determine how to handle this. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact work.