Elias Johannes
Elias Johannes

Reputation: 734

ASP.NET Razor Passing full object in form

I've got a very basic problem but it seems that i can't find a solution.

I have an object that is initalized in my OnGet method. I want the Client to update the information and then update it back to the DB. (Some of the information of the object are only stored in the DB but are not visible or editable to the User)

Now i'm trying to pass the full object in my form, combined with the inputs from the Client. If i try to access the object all of the attributes are null. Instead if I'm trying to only pass a single attribute as a string in the form, it works just fine.

This is my code:

public class UpdateLicenseModel : PageModel
{
    private readonly license_backupContext _db;
    public UpdateLicenseModel(license_backupContext db)
    {
        _db = db;
    }
    public IList<License> Licenses { get; set; }

    [BindProperty]
    public License SelectedLicense { get; set; }


    [Auhtorize]
    public void OnGet()
    {
        Licenses = _db.License.ToList();
        SelectedLicense = Licenses.SingleOrDefault(s => s.Licenseid.ToString().Equals(LicenseString));
    }

    public IActionResult OnPost()
    {
        var license = SelectedLicense;
        if (!ModelState.IsValid)
        {
            return Page();
        }
        _db.License.Update(license);
        _db.SaveChanges();

        return RedirectToPage("./Licenses");
    }
}

In the code above, the license member in OnPost only has values in the fields, where the user edited something, all other fields are null (Shouldn't be null)

This is my form (simplified):

<form method="post">
<div class="row">
    <div class="col-6 form-group">
        <label for="ID"><b>ID</b></label>
        <input class="form-control input-sm" id="ID" value="@Model.SelectedLicense.ID" asp-for="SelectedLicense.ID" />
    </div>
    <div class="col-6 form-group">
        <label for="Name"><b>Name</b></label>
        <input class="form-control input-sm" id="Name" value="@Model.SelectedLicense.Name" asp-for="SelectedLicense.Name" />
    </div>
</div>
<div class="row">
    <div class="col">
        <input type="submit" class="btn btn-success" value="Speichern" />
    </div>
</div>
<input type="hidden" asp-for="SelectedLicense" />
</form>

Upvotes: 0

Views: 2244

Answers (1)

Mike Brind
Mike Brind

Reputation: 30045

You can't post .NET objects in HTML forms in the way that you have tried. You can only post their member values and let ModelBinding construct the object for you on the server.

Usually, you would use the hidden field to store the key value of the selected object, and then use that value to determine which row of data needs updating in the database.

Or you could potentially serialise the SelectedLicense to JSON and then encode that using Base64 or similar, and store that in the hidden field. Then you can decode and deserialise it on the server. However, savvy users will have access to the value in the browser and could decode it themselves.

Upvotes: 3

Related Questions