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

  • Home
  • SEARCH
  • 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 8227419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:07:30+00:00 2026-06-07T16:07:30+00:00

I’m new in asp.net mvc3 programming and I’m trying to build a specific form.

  • 0

I’m new in asp.net mvc3 programming and I’m trying to build a specific form. I need to have a form with the user field (which I have) but also a list of object (in that case SStatus).


My form :

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Création d'utilisateur</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Lastname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Lastname)
            @Html.ValidationMessageFor(model => model.Lastname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Firstname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Firstname)
            @Html.ValidationMessageFor(model => model.Firstname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Login)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Login)
            @Html.ValidationMessageFor(model => model.Login)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <p>Status</p>
        @{

//The error was form here

                @{
                    var list = ViewBag.listStatus as List<SStatus>;
                }
                @if (list != null)
                {
                    foreach(var status in list)
                    {
                        <option value=@status.ID>@status.Name</option> 
                    }
                }
            </select>
        }

        <p>
            <input type="submit" value="Création" />
        </p>
    </fieldset>
}

The list call :


public ActionResult CreateUserView()
        {
            RestClient client = new RestClient(Resource.Resource.LocalUrlService);
            RestRequest request = new RestRequest("/status/all", Method.GET);
            var response = client.Execute(request);
            if(response.StatusCode == HttpStatusCode.OK)
            {
                List<SStatus> listSatus = JsonHelper.FromJson<List<SStatus>>(response.Content);
                ViewBag.listStatus = listSatus;
            }
            return View();
        }

And the form post:


     [HttpPost]
            public ActionResult CreateUserView(Uuser userToCreate, string list)
            {
//list got the ID of SStatus. 
                if (ModelState.IsValid)
                {//Stuff}
    }

So the question is : How get the selected list item ?

Regards.

  • 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-06-07T16:07:31+00:00Added an answer on June 7, 2026 at 4:07 pm

    Use a view model pattern. I still don’t see how your Uuser object is sent to the view (via the default [HttpGet] action, but I think I see what you’re trying to accomplish.) If you refactor this way, you’ll still get to use the built-in validation, automagic model binding, etc.

    public class CreateUserViewModel
    {
        public Uuser User { get; set; }
        public string Status { get; set; }
    }
    

    Then your action parameter should be of type CreateUserViewModel e.g.

    [HttpPost] 
    public ActionResult CreateUserView(CreateUserViewModel vm)
    {
        if(ModelState.IsValid)
        {
            {//Stuff}
    }
    

    I believe you’ll need a name attribute on the <select> element in order for it to be posted.

    <p>Status</p>
        @{
            <select name="Status">
    

    Although, you’re going to run into trouble if the model isn’t valid. Your view should be strongly typed against CreateUserViewModel e.g.

    @model YourModelNamespace.CreateUserViewModel
    

    So, your Lastname property might look like this (note the .User)

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Création d'utilisateur</legend>
    
            <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>
    

    And finally, I guess you could keep the possible list of status in the ViewBag, but you’ll want to set the selected value to @Model.Status. You may want to consider changing CreateUserViewModel.Status to a List<SelectListItem> that you can populate from your controller e.g. your GET action should return View(CreateUserViewModel)

    public ActionResult CreateUserViewModel()
    {
        CreateUserViewModel vm = new CreateUserViewModel();
        vm.User = // set user
        vm.Status = new List<SelectListItem>()
        {
            new SelectListItem()
            {
                Value = "status1",
                Text = "status 1",
                Selected = false
            },
            new SelectListItem()
            {
                Value = "status2",
                Text = "status 2",
                Selected = true
            },
        };
    
        return View(vm); // this is the correct way to strongly type your view
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.