Home > Back-end >  Send model parameters obtained from GET to POST method?
Send model parameters obtained from GET to POST method?

Time:10-24

I'm new working on ASP.Net Core MVC application with razor view. So I have a simple view profile with HttpGet and HttpPost controller as:

[HttpGet]
public async Task<IActionResult> Index()
{
    var userEmail = User.FindFirstValue(ClaimTypes.Name);
    var currentUser = await _userManager.FindByEmailAsync(userEmail);
    var roles = await _userManager.GetRolesAsync(currentUser);

    var user = new UserViewModel
    {
        Username = $"{currentUser.FirstName} {currentUser.LastName}",
        NameAbbrv = $"{currentUser.FirstName[..1]}{currentUser.LastName[..1]}",
        Roles = string.Join(", ", roles),
        Email = currentUser.Email
    };
    return View(user);
}

As you can see the get method return user model to view, so in my view I have something like:

@model MyProject.Web.Models.ProfilesViewModel.UserViewModel

And I use properties as:

 <h5>@Model.Username</h5>
 <p>@Model.Roles<br/>@Model.Email</p>

The ViewModel:

public class UserViewModel
{
    public string? Username { get; set; } = string.Empty;

    public string? NameAbbrv { get; set; } = string.Empty;

    public string? Roles { get; set; } = string.Empty;

    public string Email { get; set; } = string.Empty;

    public ChangePasswordViewModel ChangePassword { get; set; } = new ChangePasswordViewModel();
}

As you can see in ViewModel I initialize an empty view model ChangePasswordViewModel, I use that ViewModel to populate the form method I have in the view, so in the view I have something like:

<form asp-controller="Profiles" asp-action="Index" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post"  role="form" novalidate>

 <h5>@Model.Username</h5>
 <p>@Model.Roles<br/>@Model.Email</p>
 <input asp-for="ChangePassword.OldPassword"  id="currentPassword"   required autofocus />
<input asp-for="ChangePassword.NewPassword" id="newPassword"  required autofocus />
</form>

So, on the submit it executes the post method with ChangePasswordViewModel populated from inputs inside the form. BUT it does not return the curren user properties that had on the Get of view.

How can I return the populated GET properties when I the POST method is executed? I mean, I can execute again the properties as the GET method as:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Index(UserViewModel model)
 var userEmail = User.FindFirstValue(ClaimTypes.Name);
    var currentUser = await _userManager.FindByEmailAsync(userEmail);
    var roles = await _userManager.GetRolesAsync(currentUser);
    
    model.Email = currentUser.Email
    ... /etc

   }

But I'm going to repeat code, is there a way to send it data from the view to the POST method?

CodePudding user response:

How about try to use hidden?

<input type="hidden" asp-for="Username" />
<input type="hidden" asp-for="Roles" />
 <input type="hidden" asp-for="Email" />
  • Related