ASP.NET Core MVC app : I want to show the same view after submit and render the same data but having problems

I tested your code and found that it runs very well in my project.

Except for TimeDue, the field is not successfully bound, the rest of the fields are bound successfully.

I don’t know if you have the same problem. The TimeDue is not successful binding is because you set the Value in the view.

Please delete the value in this field in the view:

 <input asp-for="TimeDue" class="form-control" />

Result: enter image description here

By the way,your User is null only because your User is not successfully bound, you can modify your loop code as follows:

 @for (int i = 0; i < Model.User.Count(); i++)
    {
        <input type="hidden" name="User[@i].Id" [email protected][i].Id />
        <input type="hidden" name="User[@i].Name" [email protected][i].Name />
    }

Then in your Create action:

[HttpPost]
    public IActionResult Create(ChloramineLogEditSaveViewModel clEditSaveViewModel)
    {
       
        return View(clEditSaveViewModel);
    }

Result: enter image description here

Edit:

My codes:

Controller:

  public IActionResult Create()
    {
        var model = new ChloramineLogEditSaveViewModel
        {
            Id = 1,
            Comments = "aaaa",
            PostPrimaryCarbanTankTp1 = "fjsdgk",
            TimeDue = "bbbbbb",
            IsCompleted=true,
            RnSelected="gggg",
            Selected="sgs",
            User=new List<User>
            {
                new User{
                Id=1,
                Name="aa"
                },
                new User
                {
                   Id=2,
                   Name="bb"
                }
            }
        };
        return View(model);
    }
    [HttpPost]
    public IActionResult Create(ChloramineLogEditSaveViewModel clEditSaveViewModel)
    {
       
        return View(clEditSaveViewModel);
    }

View:

<form asp-action="Create">
  <div class="form-group row">
    @for (int i = 0; i < Model.User.Count(); i++)
    {
        <input type="hidden" name="User[@i].Id" [email protected][i].Id />
        <input type="hidden" name="User[@i].Name" [email protected][i].Name />
    }


    <label asp-for="TimeDue" class="col-sm-2 col-form-label control-label"></label>

    <div class="col-sm-8">

        <input asp-for="TimeDue" class="form-control" />
    </div>
</div>
<div class="form-group row">
    <label asp-for="PostPrimaryCarbanTankTp1" class="col-sm-2 col-form-label"></label>
    <div class="col-sm-8">
        <div class="form-check">
            <label class="form-check-label">

                <input class="form-check-input" asp-for="PostPrimaryCarbanTankTp1" />
            </label>
        </div>
    </div>
</div>
<div class="form-group row">
    <label asp-for="Comments" class="col-sm-2 col-form-label"></label>
    <div class="col-sm-8">
        <input asp-for="Comments" class="form-control" />
    </div>

</div>
<div class="form-group row">
    <label asp-for="IsCompleted" class="col-sm-2 col-form-label"></label>
    <div class="col-sm-8">
        <div class="form-check">
            <input class="form-check-input" asp-for="IsCompleted" />
        </div>
    </div>
</div>

<div class="form-group row">
    <label class="col-sm-2 col-form-label">Technician</label>
    <div class="col-sm-8">
        <select asp-for="Selected" [email protected](new SelectList(Model.User,"Id","Name"))>
            <option value="">Select...</option>
        </select>
    </div>
</div>

<div class="form-group row">
    <label class="col-sm-2 col-form-label">RN</label>
    <div class="col-sm-8">
        <select asp-for="RnSelected" [email protected](new SelectList(Model.User,"Id","Name"))>
            <option value="">Select...</option>
        </select>
    </div>
</div>

<div class="form-group row">
    <label class="col-sm-2 col-form-label"></label>
    <div class="col-sm-8">
        <input type="submit" value="Create" class="btn btn-secondary" />
    </div>
</div>
<div><span asp-validation-for="TimeDue" class="text-danger"></span></div>
<div><span asp-validation-for="Comments" class="text-danger"></span></div>
<div><span asp-validation-for="IsCompleted" class="text-danger"></span></div>
</form>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top