Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4254364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:04:26+00:00 2026-05-21T05:04:26+00:00

I am trapped in ViewModel binding and Refresh scenario. Scenario: As Administrator I want

  • 0

I am trapped in ViewModel binding and Refresh scenario.

Scenario:
As Administrator I want to create User Account and based on selected Role from DropDownList refresh FORM to select by checkboxes specific accesses (bool) connected with Role.

So user can have one single role with default access list that can be modified by Admin when he create Account.

My ViewModel:

public class CreateViewModel
{
    public User User { get; set; }
    public IEnumerable<Role> Roles { get; set; }
    public IEnumerable<RoleAccess> RoleAcceses { get; set; }
    public Role SelectedRole { get; set; }


    public void Refresh()
    {
        User.UserAccesses.Clear();
        foreach (var item in RoleAcceses.Where(x => x.RoleId == SelectedRole.Id))
        {
            User.UserAccesses.Add(new UserAccess { UserId = User.Id, Access = item.Access, AccessId = item.AccessId, Value = item.Value });
        }
    }
}

As you can see in above code I have RoleAccess collection with default values for specific Role. Refresh method clears actual UserAccess collection and repopulate it for SelectedRole.

My questions are:

  1. Is it possible to do this without jQuery? What is your opinion?
  2. If it is possible to refresh FORM without jQuery how to post it without loosing Binded values entered by Admin?
    Here I can NOT Add other @BeginForm because I will lose actual data context right?
  3. If jQuery is only solution how to write Controller Action that would check for jQuery Filtering ?
  4. How jQuery function should looks like to replace Binded to ViewModel checkboxes?

My courrent controller Action:

public class AccountController : Controller
{
    IAccountService accountService;

    public AccountController()
    {
        accountService = new AccountService();
    }

    [HttpGet]
    public ActionResult Create(int role = 1)
    {
        IEnumerable<RoleAccess> roleAccesses = null;
        IEnumerable<Role> roles = null;

        roleAccesses = accountService.GetRoleAcgesses();
        roles = accountService.GetRoles();

        var createViewModel = new CreateViewModel();
        createViewModel.RoleAcceses = accountService.GetRoleAccesses();
        createViewModel.Roles = accountService.GetRoles();
        createViewModel.SelectedRole = createViewModel.Roles.FirstOrDefault(x => x.Id == role);
        createViewModel.Refresh();

        return View(createViewModel);
    }

    public ActionResult Refresh(int role)
    {
        return RedirectToAction("Create", "Account", new { role });
    }

    [HttpPost]
    public ActionResult Create([Bind(Prefix = "User")] Models.User user, FormCollection formsCollection)
    {
        var viewModel = new CreateViewModel();
        UpdateModel<CreateViewModel>(viewModel, new[] { "User.Login", "User.Password", "User.FirstName", "User.LastName" });

        viewModel.User.RoleId = Convert.ToInt32(formsCollection["Role"]);

        if (ModelState.IsValid)
        {
            bool result = accountService.CreateAccount(viewModel.User);

            if (!result)
            {
                // SEND INFO ABOUT FAILURE
            }
            else
            {
                return RedirectToAction("list");
            }
        }

        return View();
    }

    [HttpGet]
    public ActionResult List()
    {
        var accounts = accountService.GetAccounts();
        var viewModel = new ListViewModel() { Accounts = accounts };
        return View(viewModel);
    }
}

My View:

@model VDOT.Web.AccountViewModels.CreateViewModel
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("create", "account"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    <legend>CreateViewModel</legend>
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(model => model.User.Login)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.Login)
            @Html.ValidationMessageFor(model => model.User.Login)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.User.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.Password)
            @Html.ValidationMessageFor(model => model.User.Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.User.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.FirstName)
            @Html.ValidationMessageFor(model => model.User.FirstName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.User.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.User.LastName)
            @Html.ValidationMessageFor(model => model.User.LastName)
        </div>
        <div class="editor-field">
            <select id="paging" onchange="location.href=this.value">
                @foreach (var item in Model.Roles)
                {
                    <option value="@Url.Action("create", "account", new { role = item.Id })">
                        @item.Name
                    </option>
                }
            </select>
        </div>
        <div class="editor-field">
            @foreach (var item in Model.User.UserAccesses)
            {
                @Html.CheckBox(item.Access.Name, item.Value);
                <br />
            }
        </div>
    </fieldset>
</fieldset>
<fieldset>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

Thank you for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:04:27+00:00Added an answer on May 21, 2026 at 5:04 am

    I think you are looking for a way to bind your user accesses collection to your user view model. With this in place your complete model would be persistent on post back allowing you to achieve whatever functionality required without scripting (although you could then use jQuery, if available, to enhance the page for “smoother” functionality).

    A great method for creating model list property bindings is available on Steven Sanderson’s blog, here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm now trapped by a performance optimization lab in the book Computer System from
I'm coming from the Winforms world where I trapped everything and handled errors with
I have an app that I want to be launchable either explicitly when tapped
Submitted for your approval, a story about a poor little java process trapped in
I know it may be simple question to all experienced developer.. I have trapped
Is there a way to start an activity from a view? I have this
I am learning Erlang and I am trying to create a very sample blog
I am sending a new logon and password to a user, however when I
I am converting an extremely large and very old (25 years!) program from C
As shown above, this trapped me for a long time. This is a matrix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.