I am having this problem when i press a link to view data in pdf:
System.NullReferenceException: 'Object reference not set as an instance of an object.'
This problem arises when I try to display the SQL values in PDF.
As you can see in the image, there is a link to see the values in PDF, when you click there the error already mentioned will appear.
I share the code:
Controller:
public ActionResult Index(string buscarTitulo)
{
PDFPrinter db = new PDFPrinter();
ViewBag.CurrentFilter = buscarTitulo;
var datos = from s in db.SQLs
select s;
if (!String.IsNullOrEmpty(buscarTitulo))
{
datos = datos.Where(s => s.Titulo.ToString().Contains(buscarTitulo.ToUpper()));
}
return View(datos.ToList());
}
public ActionResult Pdf()
{
var reporte = new ActionAsPdf("Index");
return reporte;
}
public ActionResult Impresion(double? tit) {
using (PDFPrinter db = new PDFPrinter()) {
V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
List<V_CuetaWeb> lista = new List<V_CuetaWeb>();
lista.Add(v);
var reporte = new PartialViewAsPdf("Pdf", v);
return reporte;
}
}
Index:
#model IEnumerable<ProvidusCuotas.V_CuetaWeb>
#{
ViewBag.Title = "Inicio";
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
#using (Html.BeginForm())
{
<p>
Título: #Html.TextBox("buscar", ViewBag.CurrentFilter as string)
<input type="submit" value="Filtrar" /><br />
<input type="button" value="Imprimir" onclick="window.print()" />
</p>
}
</div>
<div>
<table border="1">
#foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Suscriptor">Suscriptor: </th>
<td>
<b>#Html.DisplayFor(modelItem => item.Apellido), #Html.DisplayFor(modelItem => item.Nombre)</b>
</td>
<td>Título: #Html.DisplayFor(modelItem => item.Titulo)</td>
</tr>
PDF:
#model IEnumerable<ProvidusCuotas.V_CuetaWeb>
#{
ViewBag.Title = "PDF";
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
<table border="1">
#foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Domicilio">Domicilio: </th>
<td>
#Html.DisplayFor(modelItem => item.Domicilio)
</td>
<td></td>
<td></td>
<td></td>
<td>Valor Nominal: #Html.DisplayFor(modelItem => item.ValNom)</td>
</tr>
<tr>
<th scope="row" abbr="Barrio">Barrio: </th>
<td>
#Html.DisplayFor(modelItem => item.Barrio)
</td>
</tr>
<tr>
<th scope="row" abbr="Localidad">Localidad: </th>
<td>
#Html.DisplayFor(modelItem => item.Localidad)
</td>
</tr>
<tr>
<th scope="row" abbr="Telefono">Teléfono: </th>
<td>
#Html.DisplayFor(modelItem => item.Telefono)
</td>
</tr>
<tr>
<th scope="row" abbr="Celular">Celular: </th>
<td>
#Html.DisplayFor(modelItem => item.Celular)
</td>
</tr>
<tr>
<th scope="row" abbr="Descripcion">D. Plan Actual: </th>
<td>
#Html.DisplayFor(modelItem => item.DescPlanActual)
</td>
</tr>
<tr>
<th scope="row" abbr="Fecha">Fecha Sorteo: </th>
<td>
#Html.DisplayFor(modelItem => item.FechaSorteo)
</td>
</tr>
<tr>
<th>Zona: #Html.DisplayFor(modelItem => item.acidzona)</th>
<th>Cobrador: #Html.DisplayFor(modelItem => item.Cobrador)</th>
<th>Código: #Html.DisplayFor(modelItem => item.Codigo)</th>
<th>Título: #Html.DisplayFor(modelItem => item.Titulo)/#Html.DisplayFor(modelItem => item.Endoso)</th>
<th>Sorteo: #Html.DisplayFor(modelItem => item.Sorteo)</th>
<th>Cuota: #Html.DisplayFor(modelItem => item.Cuota)</th>
<th>Vencimiento: #Html.DisplayFor(modelItem => item.Vencimiento)</th>
<th>Monto: #Html.DisplayFor(modelItem => item.Monto)</th>
</tr>
}
</table>
</div>
</form>
</body>
</html>
PDFPrinter class:
public partial class PDFPrinter : DbContext
{
public PDFPrinter()
: base("name=VisorPDF")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
//public virtual DbSet<V_CuetaWeb> V_CuetaWeb { get; set; }
public virtual DbSet<V_CuetaWeb> SQLs { get; set; }
}
Any sugerence? how to solve this?
I don't understan the mistake
I think your flow needs to change, so index will pass list of pdf objects. and it iterate from view and display a table. This part correct.
Then you click from one link, then it should pass that click object to second view. Then it will display you object values.
So pdf view model should be
#model ProvidusCuotas.V_CuetaWeb
Then no need to have foreach loop. Just access the properties and display those.
and you c# method should be like this
public ActionResult Impresion(double? tit) {
using (PDFPrinter db = new PDFPrinter())
{
V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
return View("Pdf", v);
}
}
Related
I working on asp.net mvc application by csharp . i face issue I can't find controls search input type textbox .
I search on action controller PendingRequests i can't found it .
also i search on index.html it not exist
so How to get search text box please ?
action controller
public ActionResult Index(string filenumber)
{
int employeeFileNo;
string userRole;
if (!string.IsNullOrEmpty(filenumber))
{
if (int.TryParse(filenumber, out employeeFileNo))
{
JDEUtility jde = new JDEUtility();
userRole = Workforce.GetUserRole(employeeFileNo);
if (!string.IsNullOrEmpty(userRole))
{
var EmpName = jde.GetEmployeeName(employeeFileNo);
Session[SessionKeys.UserCode] = employeeFileNo;
Session[SessionKeys.Username] = EmpName;
Session[SessionKeys.RoleCode] = userRole;
if (userRole != RoleKey.REQ)
return RedirectToAction("PendingRequests", "WorkforceRequests", null);
else
return RedirectToAction("MyRequests", "WorkforceRequests", null);
}
else
ViewBag.errorMsg = "unauthorized user";
}
else
{
ViewBag.errorMsg = "incorrect file no";
}
}
else
{
ViewBag.errorMsg = "unauthorized user";
}
return View();
}
pending request view
#model IEnumerable<HR.WorkforceRequisition.Models.WorkforceRequest>
#{
ViewBag.Title = "Pending Requests";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Pending Requests</h2>
<hr />
#if (!string.IsNullOrWhiteSpace(ViewBag.msg))
{
<div class="alert alert-success">
#ViewBag.msg
</div>
}
#if (!string.IsNullOrWhiteSpace(ViewBag.errorMsg))
{
<div class="alert alert-danger">
#ViewBag.errorMsg
</div>
}
<table id="dtbl" class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th></th>
<th>
#Html.DisplayNameFor(model => model.WorkforceRequestID)
</th>
<th>
#Html.DisplayNameFor(model => model.DepartmentCode)
</th>
<th>
#Html.DisplayNameFor(model => model.Section)
</th>
<th>
#Html.DisplayNameFor(model => model.RequiredPosition)
</th>
<th>
#Html.DisplayNameFor(model => model.JobTitle)
</th>
<th>
#Html.DisplayNameFor(model => model.ReportingTo)
</th>
<th>
#Html.DisplayNameFor(model => model.NationalityCategory)
</th>
<th>
#Html.DisplayNameFor(model => model.StatusRemark)
</th>
<th>
Requestor
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink("Details", "Details", new { id = item.WorkforceRequestID })
</td>
<td>
#Html.DisplayFor(modelItem => item.WorkforceRequestID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DepartmentCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.Section)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequiredPosition)
</td>
<td>
#Html.DisplayFor(modelItem => item.JobTitle)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReportingTo)
</td>
<td>
#Html.DisplayFor(modelItem => item.NationalityCategory)
</td>
<td>
#Html.DisplayFor(modelItem => item.StatusRemark)
</td>
<td>
#Html.DisplayFor(modelItem => item.CreatedByName)
</td>
</tr>
}
</tbody>
</table>
<script>
$(document).ready(function () {
$('#dtbl').DataTable({
"scrollX": true,
"pageLength": 10,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel'
]
});
});
</script>
expected result is found search text box input on which place
display text box on pending requests
From the screenshot, it seems you need search text box in the data table. The search box appears by default as such unless you have set any property to not show it. With your js I am able to see the search box You can try adding "searching": true property to your data table also. You can check the documentation here.
Here is a working snippet
$(document).ready(function () {
$('#tableID').DataTable({
// Enable the searching
// of the DataTable
searching: true // will work without this also
});
});
<!-- jQuery -->
<script type="text/javascript"
src="https://code.jquery.com/jquery-3.5.1.js">
</script>
<!-- DataTables CSS -->
<link rel="stylesheet" href=
"https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css">
<!-- DataTables JS -->
<script src=
"https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js">
</script>
<table id="tableID" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Samer</td>
<td>35</td>
</tr>
<tr>
<td>2</td>
<td>Rajiv</td>
<td>30</td>
</tr>
<tr>
<td>3</td>
<td>Suhail</td>
<td>45</td>
</tr>
<tr>
<td>4</td>
<td>Manisha</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>Robbert</td>
<td>68</td>
</tr>
<tr>
<td>6</td>
<td>Mike</td>
<td>25</td>
</tr>
<tr>
<td>7</td>
<td>Ere</td>
<td>23</td>
</tr>
<tr>
<td>8</td>
<td>James</td>
<td>35</td>
</tr>
<tr>
<td>9</td>
<td>Walt</td>
<td>65</td>
</tr>
<tr>
<td>10</td>
<td>Jessieca</td>
<td>28</td>
</tr>
<tr>
<td>11</td>
<td>Raghu</td>
<td>20</td>
</tr>
</tbody>
</table>
I'm a little confused, I'm trying to use a jQuery datatable in my ASP.NET MVC application but I was not able to add the EDIT or DELETE buttons
This is what I'm trying to convert:
<td>
#Html.ActionLink("EDIT", "Edit", new { id = item.code })
</td>
I am trying to use a DATATABLE grid.
This is my whole Index.cshtml:
#model IEnumerable<Approvers>
#{
ViewData["Title"] = "Approver";
}
<h1>Approver</h1>
<table class="table" id="Exampledatatable">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.CODE)
</th>
<th>
#Html.DisplayNameFor(model => model.NAME)
</th>
<th>
#Html.DisplayNameFor(model => model.FIRSTNAME)
</th>
<th>
#Html.DisplayNameFor(model => model.DESCRIPTION)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.FIRSTNAME)
</td>
<td>
#Html.DisplayFor(modelItem => item.DESCRIPTION)
</td>
</tr>
}
</tbody>
</table>
#section scripts{
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.23/af-2.3.5/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.23/af-2.3.5/datatables.min.js"></script>
<script>
$(document).ready(function () {
$('#Exampledatatable').DataTable({
"mDataProp": function (source, type, val) {
return '<a href= "/Home/Edit" + source.><>'
}
}
});
});
</script>
}
How i am supposed to convert that?
I have a index page the code looks like this
#model PagedList.IPagedList<PartialViews.Models.Customer>
#using PagedList.Mvc
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<div class="container">
<table class="table">
<tr>
<th>
Title
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Email Address
</th>
<th>
Phone Number
</th>
<th>
Modified Date
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmailAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CustomerID }) |
#Html.ActionLink("Details", "Details", new { id = item.CustomerID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.CustomerID })
</td>
</tr>
}
</table>
#Html.PagedListPager(Model,page=>Url.Action("Index",new{page,pageSize=Model.PageSize}))
</div>
My CustomersController has this code
private AdventureWorksLT2012Entities db = new AdventureWorksLT2012Entities();
// GET: Customers
public ActionResult Index(int page=1, int pageSize=10)
{
List<Customer> customers = db.Customers.ToList();
PagedList<Customer> cust = new PagedList<Customer>(customers,page,pageSize);
return View(cust);
//return View(db.Customers.ToList());
}
Heres the Main page where i want the Customers List from the Index action to be shown
#model PartialViews.Models.Customer
#{
ViewBag.Title = "Main";
}
<body>
<div class="container">
<h2>Customer's List</h2>
<div id="dvCustomerDetails" style="width: 50%; height: 130px;display: none"></div>
</div>
</body>
<script type="text/javascript">
$.ajax({
url: 'Customers/Index',
contentType: 'application/html;',
type: 'GET',
dataType:'html'
})
.success(function(result) {
$('#dvCustomerDetails').html(result);
})
</script>
I want to display this table onto a new page as a partial view using ajax jquery but I am having trouble implementing it.
I was able to use RenderPartial method to display it as a partial view but i am having trouble doing it with ajax. Any help or suggestion will be appreciated.
Here is the Model:
public class TestModel
{
public string Data { get; set; }
public List<string> Datas { get; set; }
}
Here is the view:
#model InventoryWeb.Models.TestModel
#using (Html.BeginForm())
{
#Html.DisplayFor(modelItem => Model.Data)
#Html.HiddenFor(m => m.Data)
<table class="table">
<tr>
<th>
Data
</th>
<th></th>
</tr>
#for (int i = 0; i < Model.Datas.Count; i++)
{
#Html.Hidden("Datas["+ i + "]", Model.Datas[i])
<tr>
<td>
#Html.DisplayFor(modelItem => Model.Datas[i])
</td>
<td>
<input type="submit" value="Submit" formaction="SubmitTest" formmethod="post"/>
</td>
</tr>
}
</table>
}
How to pass the selected row number (the number of row on which user pressed the "Submit" button) to controller? TempData/Viewbag/whatever it is possible to use to pass this info to Controller site when Submitting.
The way you wrote it, it will send all the data back to server for any row.
You can insert a form in each row:
#model InventoryWeb.Models.TestModel
#Html.DisplayFor(modelItem => Model.Data)
<table class="table">
<tr>
<th>
Data
</th>
<th></th>
</tr>
#for (int i = 0; i < Model.Datas.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model.Datas[i])
</td>
<td>
#using (Html.BeginForm())
{
#Html.Hidden("rowNumber", i)
<input type="submit" value="Submit" formaction="SubmitTest" formmethod="post"/>
}
</td>
</tr>
}
</table>
But I prefer using javascript in this situations.
Edit
With JavaScript:
#using (Html.BeginForm("SubmitTest"))
{
#Html.DisplayFor(modelItem => Model.Data)
#Html.HiddenFor(m => m.Data)
<input type="hidden" id="rowNumber" name="rowNumber" />
<table class="table">
<tr>
<th>
Data
</th>
<th></th>
</tr>
#for (int i = 0; i < Model.Datas.Count; i++)
{
#Html.Hidden("Datas["+ i + "]", Model.Datas[i])
<tr>
<td>
#Html.DisplayFor(modelItem => Model.Datas[i])
</td>
<td>
<input type="button" value="Submit" onclick="SubmitForm(#i, this);"/>
</td>
</tr>
}
</table>
}
<script>
function SubmitForm(i, btn){
$("#rowNumber").val(i);
var form = $(btn).closest("form");
form.submit();
}
</script>
use <button> to replace the <input type="submit" ..>, and set the row number in the value of the <button>
#for (int i = 0; i < Model.Datas.Count; i++)
{
<tr>
<td>
#Html.DisplayFor(modelItem => Model.Datas[i])
</td>
<td>
<button name="rowNumber" value="#i">Submit</button>
</td>
</tr>
}
And if the target post Controller/Action is different the current one, instead of setting formaction and formmethod in every submit button, set it in the Html.BeginForm() like
#using(Html.BeginForm("ActionName", "ControllerName"))
{
...
}
Partial view is not rendering while passing ViewModel.. Its rendering without ViewModel.
I mean if I keep #Html.Partial("PartialClientIndex") then its rendering and when I pass ViewModel it directly going to Dispose without rendering partial view.
I'm missing something here.. could you please help in this.
Main View:
<div id="PartialClient">
#Html.Partial("PartialClientIndex", viewModelList)
</div>
Action:
[HttpPost]
public ActionResult PartialClientIndex(int? SelectedGroupId)
{
int SkipRec = 0;
int NextRec = 25;
VMClient vmclient = new VMClient();
vmclient.IEClients = db.client.Where(cl => cl.Groups.id == SelectedGroupId).OrderBy(c => c.id).Skip(SkipRec).Take(NextRec).ToList();
return PartialView("PartialClientIndex", vmclient);
}
Partial View:
#model IEnumerable<HostingManager.Models.VMClient>
<table>
<thead>
<tr>
<th style="width:25px; padding:10px;">
Group
</th>
<th class="tbContent-Name">
Client Name
</th>
<th class="tbContent-Name">
Contact Person
</th>
<th class="tbContent-Name">
Contact Email
</th >
<th></th>
</tr>
</thead>
<tbody>
#if(Model != null)
{
var x = Model.Select(c=>c.IEClients).ToList();
var y = x[0].ToList();
// var y = x[0]["item1"];
foreach (var item in y) {
<tr>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.Groups.name)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactPerson)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactPerson)
</td>
<td class="tbContent-Name">
#Html.DisplayFor(modelItem => item.contactEmail)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.id }, new { onclick = "return confirm('Are you sure you wish to delete this ?');" })
</td>
</tr>
}
}
</tbody>
It seems that the variable vmclient that you are passing as model into your partial view is of type VMClient. Though your partial view is expecting the IEnumerable<VMClient> type.
You basically have to change the model type in your partial view to the following
#model HostingManager.Models.VMClient
and adjust the way you are assigning the y variable
var y = Model.IEClients;