I created a controller and a view, to fetch data from my database an image, which is in blob format. However, the image is not loaded in the view (I call a method to call this view).
I have the controller Image (with the GetImage method), the ReadyDrive controller and the ReadyDelivery view. Within the ReadyDate view, I pass the parameter to get the ID in the GetImage method.
Controller Imagem:
public ActionResult Index()
{
return View();
}
public ActionResult GetImagem(int id)
{
Entities1 tabela = new Entities1();
byte[] BlobImg = tabela.DATABINARY.Where(p => p.ID.Equals(id)).Select(p => p.DATA).FirstOrDefault();
return File(BlobImg, "image/png");
}
Controller ProntaEntrega:
public ActionResult Index(int? reduzido=null)
{
Entities1 Estoque = new Entities1();
List<V500_ESTOQUE_PE_WEB> ProntaE = (from a in Estoque.V500_ESTOQUE_PE_WEB select a).OrderByDescending(x => x.TOTAL_KG_PE).ToList()
.Where(x => reduzido != null ? x.COD_REDUZIDO.Equals(reduzido) : true).ToList();
return View(ProntaE);
}
View ProntaEntrega (a piece of that):
<tbody>
#foreach (var item in Model)
{
<tr>
<td class="text-left" width="30%">
#Html.DisplayFor(d => item.COD_REDUZIDO)
</td>
<td class="text-left">
#Html.DisplayFor(d => item.DESC_ARTIGO)
</td>
<td class="text-right">
#Html.DisplayFor(d => item.TOTAL_KG_PE)
</td>
<td>
<img src="#Url.Action("GetImage", "Imagem", new { id = #item.IDBLOB})" width=50 />
</td>
</tr>
}
</tbody>
Somebody to help me?
Tks!
convert the blob in base64. set image src something like below
<img src="data:image/jpeg;base64,iVBORw0KGg" />
reference
Related
I am trying to display an image stored in the database (actually a varbinary) and trying to display it in a "img" tag. Simple View:
<table style="font-size:x-small">
#Html.HiddenFor(x=>x.CandidateId, new { #id = "txtCandidateId"})
<tr>
<td>
#Html.DisplayNameFor(x=>x.Name)
</td>
<td>
#Html.DisplayTextFor(x=>x.Name)
</td>
<td>
AD Account User Name:
</td>
<td>
<input type="text" id="txtUsername" />
</td>
</tr>
<tr>
<td colspan="4">
<img id="img" src="" alt="" />
</td>
</tr>
</table>
Then in script:
var cand;
$(document).ready(function () {
debugger;
cand = function () { return #Html.Raw(Json.Encode(Model)) }();
var source = "/InProcess/RetrieveImage/" + cand.CandidateId;
$("#img").attr("src", source);
});
Then in controller:
public ActionResult RetrieveImage(string candidate)
{
byte[] pdf;
using (var memStream = new MemoryStream())
{
Repository.GetCandidatePdf(candidate).CopyTo(memStream);
pdf = memStream.ToArray();
}
if (pdf != null)
{
return File(pdf, "cover/jpg");
}
else
return null;
}
I can see the correct id, which is a uniqueidentifer, in the JS of the page and the action is getting called but the candidateId is null. Anybody have any ideas why? Is there a better way to do this?
By default, the route parameters in ASP.NET MVC are {controller}, {action}, and {id} - controller and action are handled for you, and id needs to be the name of the action paramemter:
public ActionResult RetrieveImage(String id) {
...
}
Also your Content-Type is incorrect, it should be image/jpeg if you're returning a JPEG image, or application/pdf if you're returning an Adobe PDF document. However you cannot load a PDF into an <img /> element.
i want to bring the partial view of question link list to my edit view. However, when i select the groupby value, it shows the error below. Please guide me.
The model item passed into the dictionary is of type
'System.Collections.Generic.List1[<>f__AnonymousType42[SurveyTool.Models.SURV_Question_Ext_Model,SurveyTool.Models.SURV_Question_Model]]',
but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[SurveyTool.Models.SURV_Question_Ext_Model]'.
Edit view:
#model IFXSurveyTool.Models.SURV_Main_Model
<div class="question">
<h2>Link</h2>
#Html.Action("QuestionLink", "SURV_Main", new { Survey_ID = Model.Survey_ID })
</div>
Controller:
public ActionResult QuestionLink(int Survey_ID)
{
var query = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model on r.Qext_Question_ID equals s.Question_ID
where s.Question_Survey_ID == Survey_ID
group new { r, s } by r.Qext_Language into grp
select grp.FirstOrDefault();
return PartialView(query.ToList());
}
QuestionLink view:
#model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>
<br />
<table class="strip">
#foreach (var item in Model)
{
<tr>
<td width="5%"></td>
<td>
#Html.ActionLink("QuestionLink", "Edit", "SURV_Answer", new { Language = item.Qext_Language }, null)
</td>
</tr>
}
</table>
Please change following line in Your PartialView code:
#model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>
to:
#model List<SurveyTool.Models.SURV_Question_Ext_Model>
Or returning type in controller. Types needs match.
From comments:
there is also returned group of LINQ query, so it's anoumous Type, not the one You expecting.
You have little error. Try this:
#model IEnumerable<SurveyTool.Models.SURV_Question_Ext_Model>
<br />
<table class="strip">
#foreach (var item in Model)
{
<tr>
<td width="5%"></td>
<td>
#Html.ActionLink("Edit", "QuestionLink", "SURV_Answer", new { Language = item.Qext_Language }, null)
</td>
</tr>
}
The short and "straight" question:
A Controller action method (A) is called by a form with some parameters. In the same View, there is another form, which when submitted calls another action method (B) in the same controller.
How can I access the values of (B) form (but essentially they are the content values of dropdowns and textboxes) from the action method A?
The Long and winding explanation:
I have a View with two forms, like this:
using (Html.BeginForm("List", "Rapporti"))
{
<span>Ricerca Rapporti per Linea </span>
#Html.DropDownList("KLinea",
new SelectList(new RapportiCT.Domain.Entities.RapportiEntities().TLinea, "KLinea", "DLinea")
, "Scegli una linea");
<span>Data inizio</span>
#Html.TextBox("startDate", DateTime.Now.AddDays(-4).ToShortDateString(), new { #class = "dateTextBox" });
<span>Data fine</span>
#Html.TextBox("endDate", DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy"), new { #class = "dateTextBox" })
<input type="submit" value="Cerca" />
}
if (Model.Count() > 0)
{
var lastArticolo = "";
<h2>Rapporti Capoturno #Model.First().rapportoCT.DLinea</h2>
<table>
#foreach (var rapporto in Model)
{
if (lastArticolo != rapporto.rapportoCT.DCodArt)
{
<tr>
<td class="td_title">
#rapporto.rapportoCT.DCodArt
</td>
<td colspan="3" class="td_title">
#rapporto.rapportoCT.DDescArt
</td>
<td class="td_title">
Rendimento
</td>
<td class="td_title">
#Pallet control.
</td>
<td class="td_title">
#Pallet accanton.
</td>
<td class="td_title">
Ore fermo linea
</td>
</tr>
lastArticolo = rapporto.rapportoCT.DCodArt;
}
using (Html.BeginForm("ControlloQualita", "Rapporti"))
{
<tr>
<td class="td_detail">
#rapporto.rapportoCT.Data
</td>
<td class="td_detail">
#rapporto.rapportoCT.Turno
</td>
<td class="td_detail">
#rapporto.rapportoCT.Lettera
</td>
<td class="td_detail">
#rapporto.rapportoCT.DNota
</td>
<td class="td_detail">
#rapporto.RendimentoLinea
</td>
<td class="td_detail">
#Html.TextBox("PalletControllati", rapporto.PalletControllati, new { style="width:50px" })
</td>
<td class="td_detail">
#Html.TextBox("PalletAccantonati", rapporto.PalletAccantonati, new { style = "width:50px" })
</td>
<td class="td_detail">
#Html.TextBox("OreFermoLinea", rapporto.OreFermoLinea, new { style = "width:50px" })
</td>
<td>
<input type="submit" value="Salva" />
</td>
</tr>
}
}
</table>
}
else
{
#:Nessun record trovato
}
Both forms post to the same RapportiController: the first form is used to query the database and show the resulting records, with the second one I'd like to update the database record.
View (snapshot):
So, my Controller class is like this:
// No problem here
public ViewResult List(int? KLinea = null, DateTime? startDate = null, DateTime? endDate = null)
{
IQueryable<VRapportiCT> qVRapporti = repository
.ViewRapporti(KLinea.Value, startDate.Value, endDate.Value);
List<VRapportiCT> lRapporti = qVRapporti.ToList();
List<RapportoRendimentoViewModel> listRRVM = new List<RapportoRendimentoViewModel>();
foreach (var rapporto in lRapporti)
{
RapportoRendimentoViewModel rrVM = new RapportoRendimentoViewModel();
rrVM.rapportoCT = rapporto;
rrVM.RendimentoLinea = repository.GetRendimentoLinea(rapporto);
rrVM.PalletControllati = "0";
rrVM.PalletAccantonati = "0";
rrVM.OreFermoLinea = "0";
listRRVM.Add(rrVM);
}
return View(listRRVM);
}
public RedirectToRouteResult ControlloQualita(int PalletControllati, int PalletAccantonati, int OreFermoLinea)
{
// How can I access the 1°form values here (a.k.a. the DropDown and Date text box values?
// ...Fake values, only to assert that the parameters get passed to the action...
RouteValueDictionary dic = new RouteValueDictionary();
dic.Add("KLinea", 55);
dic.Add("startDate", DateTime.Now.AddDays(-4));
dic.Add("endDate", DateTime.Now);
return RedirectToAction("List", "Rapporti", dic);
}
I see 2 options here:
Store last submitted "List" form in, lets say, Session, or TempData, or any other storage you like and restore these values on "ControlloQualita" form submission. This approch assumes that you need last submitted values from "List" form rather than latest.
Intercept "ControlloQualita" form submission in order to append values of "List" form to the request. This can be achieved with javascript by adding hidden inputs to the "ControlloQualita" form.
Using MVC, in the GET function in the controller I am creating the VM and passing it to the view.
[Themed]
public ActionResult OrderManufacturedProducts(int id)
{
QBProductRecord QBproduct = _qbproductService.GetById(id);
OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);
return View(model);
}
Then The View:
#model System.ViewModels.OrderManufacturedProductsVM
#{
Script.Require("ShapesBase");
Layout.Title = T("Manufactured Orders").ToString();
}
#using (Html.BeginFormAntiForgeryPost())
{
<fieldset>
<table class="items" summary="#T("This is a table of the manufactured products to be ordered")">
<colgroup>
<col id="Col1" />
<col id="Col2" />
<col id="Col3" />
</colgroup>
<thead>
<tr>
<th scope="col"> ↓</th>
<th scope="col">#T("Name")</th>
<th scope="col">#T("Description")</th>
</tr>
</thead>
<tbody>
<tr>
<td>#Model.QBProduct.ProductName</td>
<td>#Model.QBProduct.StockLevel</td>
<td><input id="Order" name="NoToOrder" type="text" value="0" onchange=""/></td>
</tr>
</tbody>
</table>
<div class="align right"><button type="submit" name="command" value="Save">Order</button></div>
</fieldset>
}
So the user enters a order no. in the input field and clicks submit which returns to the post.
[HttpPost, ActionName("OrderManufacturedProducts")]
public ActionResult OrderManufacturedProductsPOST(int id, int NoToOrder)
{
// OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);
// return View(model);
return Index();
}
I want to return to the Index page, but it is telling me
Server Error in '/OrchardLocal' Application.
The model item passed into the dictionary is of type 'System.ViewModels.ManufacturedProductsVM', but this dictionary requires a model item of type 'System.ViewModels.OrderManufacturedProductsVM'.
So do I need to use the same VM in my post? all I want to do is reload the index page after the update is made.
NOTE: the update on the record is working fine, I just cant get the correct page to display after.
In your OrderManufacturedProductsPOST action, you should redirect to the action you want to return. Like so:
[HttpPost, ActionName("OrderManufacturedProducts")]
public ActionResult OrderManufacturedProductsPOST(int id, int NoToOrder)
{
// OrderManufacturedProductsVM model = new OrderManufacturedProductsVM(QBproduct);
// return View(model);
return RedirectToAction("Index");
}
I'm new in using MVC and I have a question about summarizing the row data in a table.
Some background:
First I collect a lot of data with an ordinary SQL statement. I use a DataTable to store the data. Then I output the data with an ordinary HTML table. This works very nicely and I have no problems with it.
I would now like to summarize all of the values for each row and display the result in the bottom row.
I know that I could do this already in the data layer. Loop through the datatable and summarize the values for the rows into a new row. Then finally append the "summary row" as the last row.
Some code:
<tr class="header">
<th>Person</th>
<th>NrOfRows</th>
</tr>
<% foreach (System.Data.DataRow row in Model.Rows) { %>
<tr>
<td><%: row["Name"].ToString()%></td>
<td><%: row["NrOfRows"].ToString()%></td>
</tr>
Could you please advice me which would be the best/easiest way to do this
Do the calculation in the ControllerAction. something like this...
public ActionResult Index()
{
var rows = this.repository.GetAll();
this.ViewData["total"] = rows.AsEnumerable().Select(o => o["NrOfRows"]).Select(o => int.Parse(o)).Sum();
return this.View(rows);
}
You should consider if you don't want to "pack" your data into a model (class). in mvc project in model section add class:
public class YourModel
{
public string Name
public int NrOfRows
public YourModel(string name, int nrOfRows)
{
Name = name;
NrOfRows = nrOfRows;
}
}
then in your controller method you do:
public ActionResult Summarize(/*parameters list*/)
{
var rows = (get data) //In here you assign here your table content
ViewData.Model = rows.Select(row => new YourModel(row["Name"], int.Parse(row["NrOfRows"])));
ViewData["nrRowCount"] = rows.Select(row => row.NrOfRows).Sum();
return View();
}
and you go to the view:
<table>
<th>Person</th>
<th>nrOfRows</th>
<%: foreach(var yourModel in Model) { :%>
<tr>
<td>yourModel.Name</td>
<td>yourModel.NrOfRows</td>
</tr>
<%: } :%>
<tr>
<td>Summary: </td>
<td> <%: ViewData["nrRowCount"] %:></td>
</tr>
</table>