简体   繁体   中英

Post two kinds of data while still using Model.IsValid in ASP.NET Core 1.0

Using the models of my database from entity framework core, I have a "Users", "UserRoles", and "Roles", so as to create a many-to-many relationship between Users and Roles, like this .

The problem is that I do not want to edit the information in the "Users" class on a separate page from the "Roles" information.

The page should look as follows: Ideal page

My jquery function attempts to parse the checkboxes into a dictionary for each of the Roles in the database.

  function submitForm() { $('#myForm').submit().bind('ajax:complete', function () { $.post( "/Admin/EditUserRoles", { SuperAdmin: $('#IsSuperAdmin').is(':checked'), Administrator: $('#IsAdmin').is(':checked'), Manager: $('#IsManager').is(':checked'), Physician: $('#IsPhysician').is(':checked') }); }); }; 
 @model Users <h1 class="col-xs-offset-4">@ViewData["Title"]</h1> <form id="myForm" asp-controller="Admin" asp-action="@ViewData["asp-action"]" method="post" class="form form-horizontal col-xs-offset-4"> <div class="alert form-group" asp-validation-summary="All"></div> <div class="form-group"> <label asp-for="Firstname">First Name: </label> <input asp-for="Firstname" class="form-control" type="text" /> </div> <div class="form-group"> <label asp-for="Lastname">Last Name: </label> <input asp-for="Lastname" class="form-control" type="text" /> </div> <div class="form-group"> <label asp-for="Email">Email: </label> <input asp-for="Email" class="form-control" type="email" /> </div> @if (ViewData["IsAD"].Equals(true)) { <div class="form-group"> <label asp-for="Username">Username (Must match username in Microsoft Active Directory): </label> <input asp-for="Username" data-val="false" data-val-required="The Username field is required" class="form-control" type="text" /> </div> } </form> <div class="col-xs-offset-4"> <h3>Roles</h3> <div class="checkbox"> <label for="IsSuperAdmin"> <input id="IsSuperAdmin" name="IsSuperAdmin" type="checkbox" checked='@ViewData["IsSuperAdmin"]' @if (ViewData["IsMe"].Equals(true)) { @Html.Raw("disabled") ; } /> Super Admin </label> </div> <div class="checkbox"> <label for="IsAdmin" class="checkbox"> <input id="IsAdmin" name="IsAdmin" type="checkbox" checked='@ViewData["IsAdmin"]' @if (ViewData["IsMe"].Equals(true)) { @Html.Raw("disabled") ; } /> Administrator </label> </div> <div class="checkbox"> <label for="IsManager" class="checkbox"> <input id="IsManager" name="IsManager" type="checkbox" checked='@ViewData["IsManager"]' /> Manager </label> </div> <div class="checkbox"> <label for="IsPhysician" class="checkbox"> <input id="IsPhysician" name="IsPhysician" type="checkbox" checked='@ViewData["IsPhysician"]' /> Physician </label> </div> <br /> <br /> <div class="form-group"> <input type="submit" value="Create" class="btn btn-success" onclick="submitForm()" readonly /> <input onclick="history.go(-1)" type="button" value="Back" class="btn btn-default btn-group-vertical" return false;" readonly /> </div> </div> 

I must find a way to pass a "Users" and a "UserRoles" object into the post action. Is there any good way to do this?

A way to have a single controller action accept multiple objects, a way to have a single controller action accept a single bundled object, or have two actions fire one-after-the-other would suffice.

Can you create a javascript obj containing your two models:

var objUsers = {};
objUsers.FirstName = "";

var objUserRoles = {};
objUserRoles.IsSuperAdmin = "";

var obj = {};
obj.user = objUsers;
obj.userRoles = objUserRoles;

$.ajax({
        url: "",
        type: "post",
        dataType: "json",
        data: JSON.stringify(obj),
        contentType: "application/json;",

Then in your controller accept your two models:

public ActionResult blah (UserRoles user, UserRoles userRoles)

There are other ways. If your form is setup correctly then you can just serialize your form:

data: $("#formName").serialize()

then just bind to your model that contains both your user and userRoles models. The name attribute will need to be used on all inputs and formatted correctly. If you use razor it will automatically format it correctly for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM