How can I show both form and Its details in single view?

Viewed 68

I have created the form and insert it data into data base. Now I want to show Single user details in same view bottom of my form. When I click on submit button record inserted into database and show in table bottom of my form. So how can I do this?

Here is my view code:

@model Edu_Form.Models.VMHiring_edu_Info

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.Raw(ViewBag.Message);

@using (Html.BeginForm("Index", "Hiring",FormMethod.Post,new {enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <h4>Hiring Eduction information form</h4>
        <hr />
        <div class="row">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
                    @Html.LabelFor(model => model.Year_Passing, htmlAttributes: new { @class = "control-label " })
                    <div class="">
                        @Html.EditorFor(model => model.Year_Passing, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Year_Passing, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    </div>
}

enter image description here

So How can I Show the single user data in table. And which action method I write the code?? I have use code first approach and Linq.

This is HttpPost Action Method:

[HttpPost]
        public ActionResult Index(VMHiring_edu_Info Tb_Edu)
        {

            String Name = Path.GetFileNameWithoutExtension(Tb_Edu.Attachment.FileName);
            String Extension = Path.GetExtension(Tb_Edu.Attachment.FileName);
            String FullName = Name + Extension;
            String FileName = Path.Combine(Server.MapPath("~/Images/"), FullName);
            
            HttpPostedFileBase File = Tb_Edu.Attachment;
            int Length = File.ContentLength;
            if (Extension.ToLower() == "jpg" || Extension.ToLower() == "jpeg" || Extension.ToLower() == "png" || Extension.ToLower() == "pdf") {
                 
                if (Length <= 400000)
                {
                    Hiring_Edu_Info db_Edu = new Hiring_Edu_Info
                    {
                        Level = Tb_Edu.Level,
                        Board_Uni_Ins = Tb_Edu.Board_Uni_Ins,
                        City = Tb_Edu.City,
                        Country = Tb_Edu.Country,
                        Year_Passing = Tb_Edu.Year_Passing,
                        Division = Tb_Edu.Division,
                        Grade = Tb_Edu.Grade,
                        CGPA = Tb_Edu.CGPA,
                        Percentage = Tb_Edu.Percentage,
                        M_Obtained = Tb_Edu.M_Obtained,
                        M_Total = Tb_Edu.M_Total,
                        Attachment = FileName,
                        IsDegreeCompleted = Tb_Edu.IsDegreeCompleted == "1" ? true : false,
                        UserId = Tb_Edu.UserId,
                        Degree_Title = Tb_Edu.Degree_Title

                    };

                    db.Hiring_Edu_Info.Add(db_Edu);
                    int a = db.SaveChanges();

                    if (a > 0)
                    {
                        ViewBag.Message = "<sript>alert('Record inserted')</sript>";
                        ModelState.Clear();
                    }
                }
            
            
            }
           
             else
            {
                 ViewBag.Message = "<sript>alert('Record not inserted')</sript>";
              }

        }

This is my model class:

public class VMHiring_edu_Info
    {
        [Required]
        
        public String Level { get; set; }

        [Required]
       
        public string Degree_Title { get; set; }

        [Required]
       
        public String Board_Uni_Ins { get; set; }

        [Required]
        
        public String City { get; set; }

        [Required]
        
        public String Country { get; set; }

        [Required]
        public String IsDegreeCompleted { get; set; }

        [Required]
        public String SUBJECTS_MAJORS { get; set; }
        [Required]
        public String Division { get; set; }
        [Required]
        public String Grade { get; set; }
        [Required]
        public String Percentage { get; set; }
        [Required]
        public String CGPA { get; set; }
        [Required]
        public String M_Obtained { get; set; }
        [Required]
        public HttpPostedFileBase Attachment { get; set; }
        [Required]
        public String M_Total { get; set; }

        public String UserId { get; set; }

        public String Year_Passing { get; set; }

    }
1 Answers

Inside if(a > 0) set the viewbag info key to true and return the View by passing Tb_Edu model into it.

if (a > 0)
{
    ViewBag.Message = "<sript>alert('Record inserted')</sript>";
    //create a viewbag and set its value to true
    ViewBag.Info = true;
    ModelState.Clear();
    //Get all the records that match to userid
    ViewBag.records = db.Hiring_Edu_Info.Where(x=>x.UserId == yourUserId).ToList();
    return View(Tb_Edu);
}

Inside View check for that ViewBag.records if records exist create a table and populate data into it like this

@using (Html.BeginForm("Index", "Hiring",FormMethod.Post,new {enctype = "multipart/form-data" }))
{
    Form elements as it is
    ...
}
//Here check for the **records count** key
@if (ViewBag.records != null && ViewBag.records.Count > 0)
{
    <table>
        @foreach (var item in ViewBag.records as IEnumerable<Hiring_Edu_Info>)
        {
            <tr>
                <td>@item.Level</td>
                <td>@item.Degree_Title</td>
                <td>@item.Board_Uni_Ins</td>
                <td>@item.City</td>
                <td>@item.Country</td>
                <td>@item.IsDegreeCompleted == "1" ? true : false</td>
                <td>@item.SUBJECT_MAJORS</td>
                <td>@item.Division</td>
                <td>@item.Grade</td>
                <td>@item.Percentage</td>
                <td>@item.CGPA</td>
                <td>@item.M_Obtained</td>
                <td>@item.M_Total</td>
                <td>@item.UserId</td>
                <td>@item.Year_Passing</td>
            </tr>
        }
    </table>
}
Related