首页 > 解决方案 > Passing ViewModel to a View not clearing down?

问题描述

I have a project wrote in ASP.Net Core 3.1. I have a view model which has a list of all my surveyors and a single model of a surveyor. The list is used in my table to show all entries and the single model is used to bind to input controls in my Razor View. On submit I all an update method in my controller and check if the id of the passed model is 0 or not. If 0 I add it to my database and if not I update that entry. This all works fine. I then return to the view with a new viewmodel. My problem is it is not clearing the single entry so all the input controls still have the previously added entry.

View:

@model SurveyorViewModel

<link href="~/CSS/Surveyor.css" rel="stylesheet" />

<form id="SurveyorList" asp-controller="Surveyor" asp-action="update" method="post" class="mt-3">
    <input type="hidden" asp-for="Surveyor.PK_Id" />
    <div id="cover" class="container vertical-center col-xl-5 col-lg-6 col-md-8 col-sm-10 mx-auto form p-4" style="box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);">
        <div id="divSurveyorList" class="table mt-2">
            <table id="surveyorList" class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>
                            <span style="font-family:Verdana; font-size: 12px; width: 150px;">Surveyors</span>
                        </th>
                        <th style="font-size: 12px; width: 40px; color:#6d6d6d;">Edit/Delete</th>
                    </tr>
                </thead>
                <tbody>
                    @if (Model != null)
                    {
                        foreach (var surveyor in Model.SurveyorList)
                        {
                            <tr style="color: #6d6d6d; font-family: Verdana; font-size:12px; font-weight:100;">
                                <td>
                                    @surveyor.FullName
                                </td>
                                <td>
                                    <div class="btn-group">
                                        <a class="btn btn-primary" asp-controller="Surveyor" asp-action="EditSurveyor" asp-route-Id=@surveyor.PK_Id><span class="fas fa-pencil-alt"></span></a>
                                        <a class="btn btn-danger" asp-controller="Surveyor" asp-action="DeleteSurveyor" asp-route-Id=@surveyor.PK_Id )><span class="fas fa-trash-alt"></span></a>
                                    </div>
                                </td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </div>
        <div class="row align-items-start mt-1">
            <div class="col-4">
                <label asp-for="Surveyor.FullName" class="col-sm-2 col-md-12 nopadding col-form-label">Full Name</label>
            </div>
            <div class="col-8">
                <input asp-for="Surveyor.FullName" class="form-control" placeholder="Full Name" />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <label asp-for="Surveyor.Address_Line_1" class="col-sm-2 col-md-4 col-form-label">Address</label>
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.Address_Line_1" class="form-control" placeholder="Address Line 1" />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.Address_Line_2" class="form-control" placeholder="Address Line 2" />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.Town" class="form-control" placeholder="Town" />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.Postcode" class="form-control" placeholder="Post Code" />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <label asp-for="Surveyor.ContactNo" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Telephone No.</label>
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.ContactNo" class="form-control" placeholder="Telephone No." />
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <label asp-for="Surveyor.Email_Address" class="col-sm-2 col-md-4 col-lg-6 col-form-label">Email Address</label>
            </div>
        </div>
        <div class="row align-items-start">
            <div class="col-12">
                <input asp-for="Surveyor.Email_Address" class="form-control" placeholder="Email Address" />
            </div>
        </div>
        <div class="d-flex flex-row-reverse">
            <button type="submit" class="btn btn-primary mt-2"><span class="fas fa-plus-circle"></span> Update</button>
            <a class="btn btn-primary float-right mt-2" asp-controller="Surveyor" asp-action="Create" style="width: 100px; margin-right: 20px;" )><span class="fas fa-plus-circle"></span><span> Create</span></a>
            <a class="btn btn-primary mt-2 mr-2" asp-controller="Surveyor" asp-action="Back" style="width: 90px;" )><span class="fas fa-hand-point-left"></span><span> Back</span></a>
        </div>
    </div>
</form>

ViewModel:

    public class SurveyorViewModel
    {
        public IEnumerable<Surveyor> SurveyorList { get; set; }
        public Surveyor Surveyor { get; set; }
    }

Model:

    public class Surveyor
    {
        [Required]
        [Key]
        public int PK_Id { get; set; }
        [Required]
        [MaxLength(120)]
        public string FullName { get; set; }
        public string Address_Line_1 { get; set; }
        public string Address_Line_2 { get; set; }
        public string Town { get; set; }
        public string Postcode { get; set; }
        public string ContactNo { get; set; }
        [EmailAddress]
        public string Email_Address { get; set; }
        public bool Archived { get; set; }
    }

Controller Methods:

        private SurveyorViewModel PopulateLists()
        {
            SurveyorViewModel SurveyorViewModel = new SurveyorViewModel();
            SurveyorViewModel.SurveyorList = context.Surveyors.ToList();
            SurveyorViewModel.Surveyor = new Surveyor();

            return SurveyorViewModel;
        }

                public IActionResult update(Surveyor surveyor)
        {

            if (ModelState.IsValid)
            {
                if (surveyor.PK_Id == 0)
                {
                     surveyor = addSurveyor(surveyor);
                }
                else
                {
                     surveyor = updateSurveyor(surveyor);
                }
            }
            SurveyorViewModel surveyorViewModel = PopulateLists();
            surveyorViewModel.Surveyor = surveyor;

            return View("Surveyor", surveyorViewModel);
        }

I've altered my update routine which when doing the add the PK_Id has a value which is passed to the view but when I inspect the elements my hidden field has a value of zero. I did a bit of research and found the following bit of code, when doing the inspect my hidden value has the correct PK_Id value but this is not passed to my controller???

            <input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />

Many thanks for any help,

标签: asp.net-coreviewbinding

解决方案


My problem is it is not clearing the single entry so all the input controls still have the previously added entry.

In the following code,you set surveyorViewModel.Surveyor with the previously added entry

   SurveyorViewModel surveyorViewModel = PopulateLists();
                surveyorViewModel.Surveyor = surveyor;
    
                return View("Surveyor", surveyorViewModel);

You can try to remove surveyorViewModel.Surveyor = surveyor;,so that the input controls will not have the previously added entry.

I've altered my update routine which when doing the add the PK_Id has a value which is passed to the view but when I inspect the elements my hidden field has a value of zero. I did a bit of research and found the following bit of code, when doing the inspect my hidden value has the correct PK_Id value but this is not passed to my controller???

Where you pass the PK_Id to view,and where you have the code <input type="hidden" value="@Model.Surveyor.PK_Id" name="PK_Id" />?


推荐阅读