0

I have a requirement to pass value from view to controller in the Html.BeginForm()

@using (Html.BeginForm("ChangeRole", "Login", null, FormMethod.Post, null, new { id = "SubmitRole" }))
{
     @Html.AntiForgeryToken()                                                                     
     @foreach (var item in (@HttpContextAccessor.HttpContext.Session.GetObjectFromJson<UserRoleLevelDetails>("GetAllUsersList")))
     {
         //..
         if (@vr != @HttpContextAccessor.HttpContext.Session.GetString("Role").ToString())
         {
          <a class="@vr" [email protected]("{0}", @item.Role_Level_Name) onclick="Change_Role(this)">@vr</a><br />
         }
        //..
    }                                                                   
}

function Change_Role(e) {
    var ChangedRole = $(e).attr('class');         
    $('#UserMainRole').val(ChangedRole);
    $("#SubmitRole").submit();
     e.preventDefault();
};

So, while submitting I need to pass value of ChangedRole to the controller.

3
  • In what way does the code shown not work as expected? What element is $('#UserMainRole') meant to reference? Is it a form element which can contain a value? Is it contained within the form? When you debug in your browser, is that element's value updated? What specifically fails? Commented Jun 29, 2022 at 13:50
  • maybe something like this: stackoverflow.com/questions/45872496/… Commented Jun 29, 2022 at 13:50
  • 1
    Can you provide a minimal reproducible unit? Commented Jun 30, 2022 at 1:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.