Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my controller

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using C3CardKYC.Models;
using System.Data.Entity.Validation;
using System.IO;

namespace C3CardKYC.Controllers
{
    public class HomeController : Controller
    {
        private ConnectString db = new ConnectString();

        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();  
        }

        public ActionResult Create()
        {

            ViewBag.doctype = new SelectList(db.DocMDs, "Id", "Nationality");

            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ActionName("create")]
        public ActionResult Create(intermediate inter)
        {
            string imagePath = null;

            try { 
            if (ModelState.IsValid)
            {
                if (inter.pimage != null)
                {
                    var uploadDir = "~/uploads";
                    imagePath = Path.Combine(Server.MapPath(uploadDir), inter.pimage);
                    var imageUrl = Path.Combine(uploadDir, inter.pimage);
                }

                var part1 = new DetailsEntry()
                {
                    ClientName = inter.ClientName,
                    EmployeeId = inter.EmployeeId,
                    EmpCitizenId = inter.EmpCitizenId,
                    EmpName = inter.EmpName,
                    Nationality = inter.Nationality,
                    DocumentType = inter.DocumentType,


                };
                var part2 = new Passport()
                {

                   pissueddate =inter.pissueddate,
                    pexpirydate=inter.pexpirydate,
                    pissuedlocation=inter.pissuedlocation,
                    pimage=inter.pimage
                };

                db.DetailsEntries.Add(part1);
                db.Passports.Add(part2);
                db.SaveChanges();
            }
                }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }

            return View();
}


        public ActionResult Delete(string[] ids)
        {
            int[] id = null;
            if(ids!=null)
            {
                id = new int[ids.Length];
                int j = 0;
                foreach(string i in ids)
                {
                    int.TryParse(i, out id[j++]);
                }
            }
            if(id!=null && id.Length >0)
            {
                List<DetailsEntry> allselected = new List<DetailsEntry>();
                allselected = db.DetailsEntries.Where(x => id.Contains(x.ClientId)).ToList();
                foreach(var i in allselected)
                {
                    db.DetailsEntries.Remove(i);

                }
                db.SaveChanges();
            }
            return RedirectToAction("displaygrid");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


        public ActionResult displaygrid()
        {
            List<DetailsEntry> details = new List<DetailsEntry>();
            details = db.DetailsEntries.ToList();

            return View(details);
        }
    }


This is my view
HTML
@model C3CardKYC.Models.intermediate
@Model C3CardKYC.Models.detailslistmodel


@{
    ViewBag.Title = "Create";

}
}
<html>
<head>
    <meta charset="utf-8">
     <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <style>
        table, td, th {
            border: 1px solid green;
            border-collapse: collapse;
            width: 30%;
        }



        th {
            border: 1px solid black;
            background-color: green;
            color: white;
        }
    </style>

    <title></title>
</head>
        <body>

        </body>


</html>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="//code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#passport").hide();

    });

    $(document).ready(function () {
        $('#MovieType').change(function () {
            var doctype = $(this).val();
            if(doctype=="0")
                $("#passport").show();


        });


    });
    </script>
    <script type="text/javascript">
    function showimagepreview(input) {

        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#image01').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#txtimg").change(function () {
        readURL(this);
    });

         </script>
 @using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>intermediate</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ClientName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ClientName)
            @Html.ValidationMessageFor(model => model.ClientName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmployeeId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmployeeId)
            @Html.ValidationMessageFor(model => model.EmployeeId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmpCitizenId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmpCitizenId)
            @Html.ValidationMessageFor(model => model.EmpCitizenId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmpName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmpName)
            @Html.ValidationMessageFor(model => model.EmpName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Nationality)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nationality)
            @Html.ValidationMessageFor(model => model.Nationality)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DocumentType)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DocumentType)
            @Html.ValidationMessageFor(model => model.DocumentType)
        </div>

       @*@*Select Document Type: @Html.DropDownList("doctype", "Select")*@
        @*@Html.DropDownListFor(model => model.ddp, new SelectList(
                  new List<Object>{
                       new { value = 0 , text = "Passport"  },
                       new { value = 1 , text = "Pan" }
                       },
                  "value",
                  "text",
                   2))*@

        <select id="MovieType" name="MovieType">


            <option  value="0" >Passport</option>

            <option value="1">Pan</option>

            <option selected="selected" value="2">Comedy</option>


        </select>


         <div id="passport">
             <div class="editor-label">
                 @Html.LabelFor(model => model.pissueddate)
             </div>
             <div class="editor-field">
                 @Html.TextBoxFor(model => model.pissueddate, new { @id = "datepicker1" })
                 @Html.ValidationMessageFor(model => model.pissueddate)
             </div>
             <div class="editor-label">
                 @Html.LabelFor(model => model.pissuedlocation)
             </div>
             <div class="editor-field">
                 @Html.EditorFor(model => model.pissuedlocation)
                 @Html.ValidationMessageFor(model => model.pissuedlocation)
             </div>

             <div class="editor-label">
                 @Html.LabelFor(model => model.pexpirydate)
             </div>
             <div class="editor-field">
                 @Html.EditorFor(model => model.pexpirydate)
                 @Html.ValidationMessageFor(model => model.pexpirydate)


             </div>

             <div class="editor-label">
                 @Html.LabelFor(model => model.pimage, new {  })
             </div>
             <div>
@*@Html.LabelFor(model => model.pimage)*@
@Html.TextBoxFor(model => model.pimage, new { type = "file", @onchange = "showimagepreview(this);", @id = "txtimg" })

 </div>
             </div>

        <p>
            <input type="submit" value="Create" />
        </p>

    </fieldset>

}

<img id="image01" style="width:200px;height:200px;" />
@Html.Partial("~/Views/Home/displaygrid.cshtml");

t

this is my displaygrid
HTML
@model IEnumerable<C3CardKYC.Models.DetailsEntry>

@{
    ViewBag.Title = "displaygrid";
    //var grid = new WebGrid(source: Model, rowsPerPage: 10);
    WebGrid grid = new WebGrid(Model);
}
<html>
<head>
    <title>WebGrid</title>

</head>
<body>
    @using (Html.BeginForm("Delete", "Home", FormMethod.Get))
    {
        @grid.GetHtml(tableStyle: "gridtable", columns: grid.Columns(grid.Column(format: @<text><input type="checkbox" name="ids" value="@item.ClientId" /></text>, header: "select"),
         grid.Column("ClientId", "ClientId"),
         grid.Column("ClientName", "ClientName"),
         grid.Column("EmployeeId", "EmployeeId"),
         grid.Column("EmpCitizenId", "EmpCitizenId"),
         grid.Column("EmpName", "EmpName"),
         grid.Column("Nationality", "Nationality"),
         grid.Column("DocumentType", "DocumentType")
 )
 )
        <input type="submit" value="delete" />

    }
</body>
</html>
<


I am getting below error A data source must be bound before this operation can be performed on mvc5. On same page i want to display one form and one webgrid. So i created one partial view and rendered inside one view but when i run subjected error is coming. Please suggest me in order to solve this error.
Posted
Updated 6-Jan-16 3:35am
v2
Comments
Maciej Los 6-Jan-16 8:41am    
What line? Have you tried to debug?
Murali Gowda 6-Jan-16 10:01am    
Why are you binding model two times?
@model C3CardKYC.Models.intermediate
@Model C3CardKYC.Models.detailslistmodel

View doesn't accept binding of model two times. Either you bind only one model or create a view model.
Member 10624821 6-Jan-16 23:50pm    
I created view model as below
public class intermediate
{

public List<detailsentry> details { get; set; }
public List<Passport> passport { get; set; }
other fields
}
@{
@Html.Partial("displaygrid",Model.details);//here error comes object not set to an instance of an object
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900