1

I got this kind of code, I have a model for my personal information and a model for the children.

In form 1, I need the model of Personal Information while in form 2 I need the model of the Children.

Here's my code.

@using HRMIS_OnlinePortal.Models
@model HRMIS_OnlinePortal.Models.Personal_Info.MainInfo
@model HRMIS_OnlinePortal.Models.Personal_Info.Child
@{
    ViewBag.Title = "PersonalInfo";
    Layout = "~/Views/Shared/_Layout.cshtml";

}  

<form class="form" role="form" autocomplete="off">
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">GSIS ID No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="GSISNumber" value="@Model.GSISNumber.ToString()" pattern="[0-9]{4}-[0-9]{4}-[0-9]{4}" />
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">Pag-ibig No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="PagibigNumber" value="@Model.PagibigNumber.ToString()" placeholder="ex. (0000-0000-0000)"/>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">Philhealth No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="PhilhealthNumber" value="@Model.PhilhealthNumber.ToString()" placeholder="ex. (00-000000000-0)"/>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">SSS No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="SSSNumber" value="@Model.SSSNumber.ToString()" placeholder="ex. (00-0000000-00)"/>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">TIN No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="TIN" value="@Model.TIN.ToString()" placeholder="ex. (123-456-789-000)"/>
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label">Agency Employee No.</label>
                                            <div class="col-lg-9">
                                                <input type="text" class="form-control" name="AgencyEmployeeNumber" value="@Model.AgencyEmployeeNumber.ToString()" />
                                            </div>
                                        </div>
                                        <div class="form-group row">
                                            <label class="col-lg-3 col-form-label form-control-label"></label>
                                            <div class="col-lg-9">
                                                <input type="button" class="btn btn-primary" value="Save">
                                                <input type="reset" class="btn btn-secondary" value="Cancel">
                                                
                                            </div>
                                        </div>
                                    </form>
                                    
                                    
                                    

and this is for the children

<div class="card-body">
                                            @using (Html.BeginForm("AddChildren", "MyProfile", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                                            {
                                                 @Html.AntiForgeryToken()
                                                <div class="form-group row">
                                                    <label class="col-md-3 col-form-label form-control-label">Surname</label>
                                                    <div class="col-md-9">
                                                        <input class="form-control NameClass" name="Surname" type="text" />
                                                    </div>
                                                </div>
                                                <div class="form-group row">
                                                    <label class="col-md-3 col-form-label form-control-label">First Name</label>
                                                    <div class="col-md-9">
                                                        <input class="form-control NameClass" name="FirstName" type="text" />
                                                    </div>
                                                </div>
                                                <div class="form-group row">
                                                    <label class="col-md-3 col-form-label form-control-label">Middle Name</label>
                                                    <div class="col-md-9">
                                                        <input class="form-control NameClass" name="MiddleName" type="text" />
                                                    </div>
                                                </div>
                                                <div class="form-group row">
                                                    <label class="col-md-3 col-form-label form-control-label">Date of Birth</label>
                                                    <div class="col-md-9">
                                                        <input class="form-control NameClass" name="DateofBirth" type="date" />
                                                    </div>
                                                </div>
                                                <div class="form-group row">
                                                    <label class="col-lg-3 col-form-label form-control-label"></label>
                                                    <div class="col-lg-9">
                                                        <input type="button" class="btn btn-primary" value="Save" onclick="Confirm()">
                                                        <input type="reset" class="btn btn-secondary" value="Cancel">
                                                    </div>
                                                </div>
                                            }
                                        </div>

It will really be a great help if anyone can explain to me how to use POST_Method in Razor MVC.

Gener Cajegas
  • 33
  • 1
  • 3
  • You can't declare two `@model` directives in a page, but you can create a parent viewmodel class that holds both viewmodel and use it with view page. Read [this](https://stackoverflow.com/questions/4764011/multiple-models-in-a-view) to see what you can do to join both viewmodels. – Tetsuya Yamamoto Jul 13 '18 at 09:06

2 Answers2

0

You'll never be able to have more than one model per view the way you have made it. However, a bit like what was said in the comments, your best bet is probably to just use your Personal_Info model as a "parent" model and access everything from there. If not, you could try experimenting with one of these solutions.

Credits to Tetsuya Yamamoto for his comment

Xariez
  • 759
  • 7
  • 24
0

Create a partial view then get children model via Query

something like this.

<div id="children"></div>
getChildren(id)
{
  $("#children").load('/Home/AddChildren/?id=' + id);
}