Home > database >  ASP.NET MVC cannot pass my hidden model back to controller
ASP.NET MVC cannot pass my hidden model back to controller

Time:11-08

model class

public int Year { get; set; } = 0;
public int Odometer { get; set; }
public string ImageURL { get; set; } = "NA";
public string Category { get; set; } = "NA";

My View

<div >
                <label >Has Vehicle Documents</label>
                @Html.DropDownListFor(Model => Model.VehDocuments, new SelectList(Model.GetYesNo()),new { @class = "form-control AutoL" })
                <span asp-validation-for="VehDocuments" ></span>
            </div>
            
            <div >
                @Html.HiddenFor(m => m.ImageURL)
                <input type="submit" value="Add New Vehicle"  />
            </div>

so if i dont have the "@Html.HiddenFor(m => m.ImageURL)" in my view then the "ImageURL" will not be passed to the controller.

using the "HiddenFor" is kind of a security issue ? as if they change the string it will mess-up the image path to the controller and save it to the DB.

How can i go around this ?

CodePudding user response:

Don't have ImageURL in the ViewModel if the user isn't supposed to see or change it.

CodePudding user response:

What you are actually asking is "How to preserve model's properties' original values when they were not edited". There are multiple options, and yes, using hidden fields is one of them. Take a look at this post on social msdn.

  • Related