user3643092
user3643092

Reputation: 436

Display checkbox Data value from database using mvc

I need to create a form to register.

the example of form will look like this

enter image description here

how do I display the value in the red circle in the form? (the value is from database)

My controller

namespace Insurance.Controllers
{
    public class FlexiPAController : Controller
    {
        //
        // GET: /FlexiPA/

        public ActionResult RegisterFlexiPA()
        {


            return View();
        }

        public ActionResult  RegisterFire()
        {
            return View();
        }

        public ActionResult RegisterMotor()
        {
            return View();
        }
    }
}

My ViewModel

namespace Insurance.ViewModels
{ 
    public class RegisterInfoPA
    {
        public register reg { get; set; }
        public infoPA infoFlexiPA { get; set; }
        public personalInfo pinfo { get; set; }
        public List<maritalInfo> minfo { get; set; }
        public childInfo cInfo { get; set; }
    }
}

My view

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

   <!--TITLE-->
    <div class="editor-label">
        Title
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.reg.title)
        @Html.ValidationMessageFor(model => model.reg.title)
    </div>
    <!--NAME-->
    <div class="editor-label">
        Name
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.reg.registerNm)
        @Html.ValidationMessageFor(model => model.reg.registerNm)
    </div>

    <!--IC NO-->
    <div class="editor-label">
        IC No
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.reg.identityNo)
        @Html.ValidationMessageFor(model => model.reg.identityNo)
    </div>

        <!--GENDER-->
<div class="editor-label">
    Gender
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.pinfo.gender)
    @Html.ValidationMessageFor(model => model.pinfo.gender)
</div>
    <!--Marital Status-->
    <div class="editor-label">
        **Marital Status**
    </div>
    <div class="editor-field">     
    </div>

Wanted to make the checkbox here for marital status eg :single,widowed,married

I am new to mvc, really appreciate your helps.

Upvotes: 1

Views: 3403

Answers (2)

user3431310
user3431310

Reputation: 871

Try this Let's try one controller first

Your Controller

namespace Insurance.Controllers
{
public class FlexiPAController : Controller
{
//
// GET: /FlexiPA/

DATABASENAME db = new DATABSENAME (); //ESTABLISHING CONNECTION TO DATABASE
public ActionResult RegisterFlexiPA()
{
        using (var dataBase = new DATABASENAME ())
        {
            var model = new RegisterInfoPA()
            {
                minfo = dataBase.maritalInfoes.ToList(),
                //IF YOU WISH TO ADD MORE                   

            };
            return View(model);
        }     
}

Your ViewModel

change you marital info to IEnurumerable instead of List

 public IEnumerable<maritalInfo> minfo { get; set; }

Your View

I would like to suggest you to make radiobutton instead of checkbox, because surely you can only choose one,eg: either single or married. Thus, it is highly preferred to use radiobutton

        <div>
        Marital Status
        @foreach (var item in Model.minfo)
        {
            @Html.RadioButtonFor(model => model.pinfo.maritalId, item.maritalId)@Html.Label(item.maritalStatNm)
        }
    </div>

This is how you display, then in the controller you just need to save it.

Upvotes: 1

Remay
Remay

Reputation: 69

You can use :

@foreach(var item in Model.minfo) { @Html.EditorFor(o => item) }

Create a partial view called maritalInfo.cshtml. Put this partial in Views/Shared/DisplayTemplates.

In the partial :

@model maritalInfo
<!--Marital Status Name and Value depend on your maritalInfo Model-->
<div class="editor-label">
    @Html.LabelFor(o => Model.Name)
</div>
<div class="editor-field">
    @Html.CheckBoxFor(o => Model.Value)     
</div>

Upvotes: 0

Related Questions