Я пытаюсь экспортировать файл Excel на основе разных таблиц в моей базе данных.Тем не менее, идентификатор, который я хочу сравнить, чтобы получить правильные результаты, всегда возвращает 0 .. URL показывает правильный идентификатор при переходе на страницу.
Кто-нибудь знает, как получить удостоверение личности?
заранее спасибо!
public ActionResult ExportExcel(int id)
{
Testmatrix testmatrix = db.Testmatrices.Find(id);
int testmatrixID = testmatrix.ID;
var sb = new StringBuilder();
var data = from s in db.Testmatrices
where s.ID == testmatrixID
select new
{
s.courseID,
s.userID,
s.testDuration
};
var list = data.ToList();
var data2 = from s in db.Subjects
where s.ID == id
select new
{
s.name
};
var list2 = data2.ToList();
var grid = new System.Web.UI.WebControls.GridView();
var grid2 = new System.Web.UI.WebControls.GridView();
grid.DataSource = list;
grid2.DataSource = list2;
grid.DataBind();
grid2.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Matrijs.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
grid.RenderControl(htw);
grid2.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
return RedirectToAction("Index");
}
Полный код контроллера следующий:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using TestgeneratorV01.Models;
namespace TestgeneratorV01.Controllers
{
public class TestmatricesController : Controller
{
private ContextModel db = new ContextModel();
// GET: Testmatrices
public ActionResult Index()
{
var testmatrices = db.Testmatrices.Include(t => t.Course).Include(t => t.User);
return View(testmatrices.ToList());
}
// GET: Testmatrices/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Testmatrix testmatrix = db.Testmatrices.Find(id);
Subject subject = db.Subjects.Find(id);
if (testmatrix == null)
{
return HttpNotFound();
}
return View(testmatrix);
}
// GET: Testmatrices/Create
public ActionResult Create()
{
ViewBag.courseID = new SelectList(db.Courses, "ID", "courseCode");
ViewBag.userID = new SelectList(db.Users.Where(x => x.userRoleID == 2), "ID", "lastName");
return View();
}
// POST: Testmatrices/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,courseID,userID,testDuration")] Testmatrix testmatrix)
{
if (ModelState.IsValid)
{
db.Testmatrices.Add(testmatrix);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.courseID = new SelectList(db.Courses, "ID", "courseCode", testmatrix.courseID);
ViewBag.userID = new SelectList(db.Users, "ID", "userEmail", testmatrix.userID);
return View(testmatrix);
}
// GET: Testmatrices/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Testmatrix testmatrix = db.Testmatrices.Find(id);
if (testmatrix == null)
{
return HttpNotFound();
}
ViewBag.courseID = new SelectList(db.Courses, "ID", "courseCode", testmatrix.courseID);
ViewBag.userID = new SelectList(db.Users, "ID", "userEmail", testmatrix.userID);
return View(testmatrix);
}
// POST: Testmatrices/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,courseID,userID,testDuration")] Testmatrix testmatrix)
{
if (ModelState.IsValid)
{
db.Entry(testmatrix).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.courseID = new SelectList(db.Courses, "ID", "courseCode", testmatrix.courseID);
ViewBag.userID = new SelectList(db.Users, "ID", "userEmail", testmatrix.userID);
return View(testmatrix);
}
// GET: Testmatrices/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Testmatrix testmatrix = db.Testmatrices.Find(id);
if (testmatrix == null)
{
return HttpNotFound();
}
return View(testmatrix);
}
// POST: Testmatrices/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Testmatrix testmatrix = db.Testmatrices.Find(id);
db.Testmatrices.Remove(testmatrix);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
public ActionResult ExportExcel(int? id)
{
Testmatrix testmatrix = db.Testmatrices.Find(id);
int testmatrixID = testmatrix.ID;
var sb = new StringBuilder();
var data = from s in db.Testmatrices
where s.ID == testmatrixID
select new
{
s.courseID,
s.userID,
s.testDuration
};
var list = data.ToList();
var data2 = from s in db.Subjects
where s.ID == id
select new
{
s.name
};
var list2 = data2.ToList();
var grid = new System.Web.UI.WebControls.GridView();
var grid2 = new System.Web.UI.WebControls.GridView();
grid.DataSource = list;
grid2.DataSource = list2;
grid.DataBind();
grid2.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Matrijs.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
grid.RenderControl(htw);
grid2.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
return RedirectToAction("Index");
}
}
}