Home > Enterprise >  dotnet 5 MVC 404 error when I post to action
dotnet 5 MVC 404 error when I post to action

Time:09-14

I have a page that is supposed to submit data for a patient evaluation. The page to submit the evaluation comes up fine. It is based on a ViewModel. Here is the ViewModel:

public class SPEPatientEvalVM
    {
        public int Id { get; set; }
        public int ApptId { get; set; }
        public int ActivityID { get; set; }
        public int VendorID { get; set; }
        public int PatientID { get; set; }
        [Required(ErrorMessage ="A selection is required.")]
        public int OverallRating { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int AppearProfessComp { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int EffGatheredInfo { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int ListenActively { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int EstabPersRapport { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int AppropExploreMyFeelings { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int AddressedMyFeelings { get; set; }
        [Required(ErrorMessage = "A selection is required.")]
        public int MetMyNeeds { get; set; }
        public string PatientComments { get; set; } = String.Empty;
        public DateTime DateSubmitted { get; set; }
    }

This is mapped and reverse mapped to the model using IMapper.

Anyway this is my controller code:

[HttpGet("[controller]/[action]/{IDAppt}/{ActivityID}/{VendorID}/{PatientID}")]
public async Task<IActionResult> AddSPEPatientEval(int IDAppt, int ActivityID, int VendorID, int PatientID)
{
    var patientChoice = GetPatientChoiceList();
    patientChoice[0].Selected = true;
    ViewBag.PatientChoice = patientChoice;

    var evalParams = await _speRepo.GetOpenPatientEval(IDAppt);

    ViewBag.Patient = evalParams.SPEPatient;

    var speEval = new SPEPatientEvalVM
    {
        ApptId = IDAppt,
        ActivityID = ActivityID,
        VendorID = VendorID,
        PatientID = PatientID
    };

    return View(speEval);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddSPEPatientEval(SPEPatientEvalVM model)
{
    var patientChoice = GetPatientChoiceList();
    patientChoice[0].Selected = true;
    ViewBag.PatientChoice = patientChoice;

    model.DateSubmitted = DateTime.Now;

    if (ModelState.IsValid)
    {
        var spePatientEval = _mapper.Map<SPEPatientEval>(model);

        var success = await _speRepo.AddSPEPatientEval(spePatientEval);
        if (!success)
        {
            return View(model);
        }
        return View("Index");
    }
    return View(model);
}

This is all for the form AddSPEPatientEval.cshtml

@model SPEPatientEvalVM

@{
    ViewData["Title"] = "AddSPEPatientEval";
}

<div >
    <h1 >Patient SPE Evaluation</h1>
    <hr />
</div>

<div >
    <div >
        <div >
            <form asp-controller="SPE" asp-action="AddSPEPatientEval" method="post">
                <div asp-validation-summary="ModelOnly" ></div>
                <input type="hidden" asp-for="ApptId" />
                <input type="hidden" asp-for="ActivityID" />
                <input type="hidden" asp-for="VendorID" />
                <input type="hidden" asp-for="PatientID" />
                <input type="hidden" asp-for="DateSubmitted" value="@String.Format("{0:MM/dd/yyyy}", DateTime.Now)" />
                <div >
                    <div >
                        <div >
                            <label asp-for="OverallRating" >As @ViewBag.Patient, rate your overall level of satisfaction with this encounter.</label>
                            <select asp-for="OverallRating" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="OverallRating" ></span>
                        </div>
                        <br />
                        <hr />
                    </div>
                </div>
                <br />
                <div >
                    <div >
                        <div >
                            <label asp-for="AppearProfessComp" >Appeared professional competent - seemed to know what s/he was
                                doing; inspired my comfidence; appeared to have my interests at heart.
                            </b></label>
                            <select asp-for="AppearProfessComp" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="AppearProfessComp" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="EffGatheredInfo" >Effectively gathered information - collected information in a way that
                                seemed organized; began with several open-ended questions and progressed through interview using a balanced ratio of open- 
                                to closed-ended questions; summarized periodically.
                            </label>
                            <select asp-for="EffGatheredInfo" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="EffGatheredInfo" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="ListenActively" >Listened actively - paid attention to both my verbal and non-verbal 
                                cues; used facial expressions/body language to express encouragement; avoided interruptions; asked questions to make sure 
                                s/he understood what I said.
                            </label>
                            <select asp-for="ListenActively" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="ListenActively" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label >Established personal rapport - introduced self warmly; verbally/non-verbally showed interest 
                                in me as a person, not just my condition; avoided technical jargon.
                            </label>
                            <select asp-for="EstabPersRapport" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="EstabPersRapport" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="AppropExploreMyFeelings" >Appropriately explored my perspective - encouraged me to 
                                identify everything that I needed to say.
                            </label>
                            <select asp-for="AppropExploreMyFeelings" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="AppropExploreMyFeelings" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="AddressedMyFeelings" >Addressed my feelings - acknowledged and demonstrated interest in my 
                                expressed and/orunexpressed feelings and experience.
                            </label>
                            <select asp-for="AddressedMyFeelings" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="AddressedMyFeelings" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="MetMyNeeds" >Met my needs - worked toward a plan which addressed both the diagnosis and 
                                my concerns about my illness.
                            </label>
                            <select asp-for="MetMyNeeds" 
                                    asp-items="@(new SelectList(ViewBag.PatientChoice, "Value", "Text"))"></select>
                            <span asp-validation-for="MetMyNeeds" ></span>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <label asp-for="PatientComments" ></label>
                            <textarea asp-for="PatientComments" 
                                      placeholder="Please add any additional comments you would like to express about this examination."></textarea>
                        </div>
                    </div>
                </div>
                <div >
                    <div >
                        <div >
                            <input type="submit" value="Submit"  />
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div >
    <div >
        <div >
            <a asp-action="Index">Back to List</a>
        </div>
    </div>
</div>

@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
    }

Once the form is completed and all required fields are entered, if I hit the "Submit" button, I get:

This localhost page can’t be found No webpage was found for the web address: https://localhost:5001/SPE/AddSPEPatientEval/18659/15129/235/4 HTTP ERROR 404

What am I missing guys? I am sure it is something that is in plain site. I am just have looked at it too long and cannot find what is going on.

What have I tried... I feel like everything. I have tried adding a route to the HttpPost statement. I have tried making sure all the model fields are not null. I do not know what else to try.

Thanks in advance for any help.

CodePudding user response:

Add [FromRoute] to the action parameters:

[HttpGet("[controller]/[action]/{IDAppt}/{ActivityID}/{VendorID}/{PatientID}")]
public async Task<IActionResult> AddSPEPatientEval([FromRoute]int IDAppt, [FromRoute] int ActivityID, [FromRoute] int VendorID, [FromRoute] int PatientID)

You should also change HttpGet to HttpPost.

CodePudding user response:

Your POST route should match the GET route: [HttpPost("[controller]/[action]/{IDAppt}/{ActivityID}/{VendorID}/{PatientID}")]

  • Related