8bitcat
8bitcat

Reputation: 2224

Add data to viewmodel from view

I'm using asp.net MVC 4. NOTE I'm using arrays for this. I know about list use em all of the time but this should be arrays.

QUESTION
Now It's a really long list and lots of checkboxes. In order to package this into a more userfriendly UI, I would like a dropbox with all certificates. And a button that says "add mandatory certificate" aswell as a button that says "add desirable certificate".

My solution(feel free to criticize)
Create part
Add two string properties to the viewmodel alternatively / or two arrays
Create a dropdownlist.
Create two buttons (one for mandatory, second for desirable)
Create two a hidden fields
Logical part
On button click add/append the value of the certificate in the dropdown list to either the hidden field for "mandatory" or "desirable".
In controller split the strings on ",", and build arrays of choosen "mandatory" / "desirable" certficates.

Is that particulary ugly or how should I do it?

I have a the following view

@using (Html.BeginForm())
{
    for (int i = 0; i <= Model.Certificates.Count - 1; i++)
    {
       @Html.HiddenFor(x => x.Certificates[i].Value)
       @Html.HiddenFor(x => x.Certificates[i].Name)
       @Html.CheckBoxFor(x => x.Certificates[i].Checked)
       @Html.LabelFor(x => x.Certificates[i].Checked, Model.Certificates[i].Name)     
       @Html.DropDownListFor(m => m.Certificates[i].SearchIncluded, 
            new SelectList(new [] 
          { new { Selected = true, Text = "Not included", Value = "Not included"},
            new  { Selected = false, Text = "Mandatory", Value = "Mandatory"}, 
            new  { Selected = false, Text = "Desirable", Value = "Desirable"},         
          }, "Value","Text","Selected"));

    <br />
}

<Input type = "submit"  Name = "SearchButton"  value = "Search"  />  

}

Upvotes: 1

Views: 1095

Answers (1)

monkeyhouse
monkeyhouse

Reputation: 2896

I guess I have a few critiques

  • create a partial view for the add a certificate form.
  • potenially create a partial view for the Certificate display within the loop, so the loop would look like

    @Html.Parital("CertificatePartial", x.Certificates[i])

  • if your forms are identical for the "Mandatory" or "Desirable" certificate, have a single "Save" button at the bottom of the Add A Certificate form. If the forms are identical, then the mandatory or desirable condition should be a property of the certificate. It can be included using a radio button on the form so the user can choose between "Mandatory" or "Desirable".

and I have another possibliiliy regaruding the way it works, but is speculation based on some things I don't really know about the intent or layout of your page

  • you could always use an ajax form post to save the new certificatation data, and then use javascript result to append it to your certification list, ie
 public ActionResult AddCertificate (AddCertificateViewModel model){
    ...save to database
    return Partial("CertificateParital", CertificateModel);
 }

you would do this by using an Ajax Form

@using(Ajax.BeginForm("action","controller",new AjaxOptions{InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "CertificationsListContainer"}))

I'm kind of assuming this works the same way as a blog post and comments.

Upvotes: 1

Related Questions