1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

5 Comments

Hi, I did like u said, but in this line, it did not allow me to do like dis @Html.EditorFor(o => Model.item) so i changed it to @Html.EditorFor(o => Model.minfo) and I got this error Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 56: </div> Line 57: <div class="editor-field"> Line 58: @foreach (var item in Model.minfo) Line 59: { Line 60: @Html.EditorFor(o => Model.minfo)
Sorry for the code, I correct it. Use @Html.EditorFor(o => item)
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 56: </div> Line 57: <div class="editor-field"> Line 58: @foreach (var item in Model.minfo) Line 59: { Line 60: @Html.EditorFor(o =>item)
still getting like the above error. i dont know why. IS there anything about the connection to database coz it is always nullreferenceException
Sure, in the controller, you need to bind your model and give it to the view (like this : RegisterInfoPa model = yourDbCnx.RegisterInfoPa.Find(id); Return View(model). Replace List<maritalInfo> by IEnumerable<maritalInfo>

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.