Display Image [VARBINARY(MAX)] in MVC - asp.net-mvc

I'm new to MVC. I got pictures stored in my table called "Resim".
I want to see the pictures when I call the associated record from the view.I will use its id to call it. Here is my code of the controller:
public ActionResult Details(int id = 0)
{
SozlukEntities db = new SozlukEntities();
KelimeTuru kelime = db.KelimeTuru.Find(id);
if (kelime == null)
{
return HttpNotFound();
}
return View(kelime);
}
Here is my code of the view:
#model SozlukRG.Models.KelimeTuru
#{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<p>Move the mouse pointer over this paragraph.</p>
<legend>KelimeTuru</legend>
<div class="div1" style="display: table; background-color: #b0c4de;">
<div class="div1" style="display: table-row;">
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayNameFor(model => model.KelimeId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayNameFor(model => model.VideoId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayNameFor(model => model.ResimId)
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayNameFor(model => model.Anlam)
</div>
</div>
<div class="div1" style="display: table-row;">
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayFor(model => model.KelimeId)
<div class="div1" style="display: table-cell; padding: 10px;">
<video width="320" height="240" controls>
<source src="/Videos/b.mp4" type="video/mp4">
</video>
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
#*The Image will be inserted here, PLEASE HELP ME WITH THIS :) *#
</div>
<div class="div1" style="display: table-cell; padding: 10px;">
#Html.DisplayFor(model => model.Anlam)
</div>
</div>
</div>
</fieldset>
<p>
#Html.ActionLink("Back to List", "Index")
</p>
Resim is the name of the table and Adi is the VARBINARY(MAX) column with the picture. I want to see the picture in the view.
Please Help..Thanks in advance...

You could create a dedicated Image controller which handles displaying images:
public class ImageController : Controller
{
public ActionResult Show(int id)
{
SozlukEntities db = new SozlukEntities();
KelimeTuru kelime = db.KelimeTuru.Find(id);
byte[] data = kelime.Aidi;
return File(data, "image/jpg");
}
}
I skipped error checking for clarity. Also, the return type might be different than "image/jpg".

Related

How to pass value of radio buttom with Html.ActionLink to controller

How can i pass my radio value with action link to controller
this is my View Page code :
foreach (var item in Model)
{
<div style="height:20px; background-color:#e1e1e1; border-radius:5px; padding:5px">
<div style="float: left; margin-right: 10px">
#Html.RadioButton("DealerRadio", item.id, new { #style = "margin-right:10px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.company, new { #style = "margin-right:30px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.address, new { #style = "margin-right:10px" })
</div>
<div style="float:right">
#Html.Label("Tell:") #Html.DisplayFor(modelItem => item.contact)
</div>
</div>
<div class="clear"></div>
}
<p>#Html.ActionLink("BUY NOW", "PreOrder", new { Dealer_id = "", Tire_id = ViewBag.Tireid }, new { #class = "btn btn-Buy" })</p>
pleas help me to do this

asp.net mvc 4 httppostedfilebase not passing to controller when using ajax modal popup

There are some similar topics in forums, but there isn't any good answer. First question is that is it possible to pass httppostedfilebase to controller, when using modal popup?
When I am using regular model and view, then everything is working well, but as soon I make this view as partialview, then everything except file is passing to controller. File is getting null value.
List View where modalpopup is excecuting:
<button style="cursor:pointer" type="submit" class="fluent-big-button" onclick="ShowModal('#Url.Action("Create", "Device")')" >
Modal Popup View:
#using (Html.BeginForm("Create", "Device", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
<fieldset>
<legend>#Resources.Insert_New_Device</legend>
<div class="grid" style="margin: 0px auto">
<div class="row" style="margin: 0px auto">
<div class="span10">
<div class="row" style="margin: 0px auto">
<div style="white-space:nowrap" class="span2 readable-text"><label>#Resources.Device_No:</label> </div>
<div class="span3">
<div class="input-control text" data-role="input-control">
#Html.TextBoxFor(m => Model.No, new { required = "required", autofocus = "autofocus", #tabindex="1"})
#Html.ValidationMessageFor(m => Model.No)
<button tabindex="-1" class="btn-clear" type="button"></button>
</div>
</div>
<div style="white-space:nowrap" class="span2 readable-text"><label>Tootja:</label> </div>
<div class="span3">
<div class="input-control text" data-role="input-control">
#Html.TextBoxFor(m => Model.Manufacturer_Name, new { autofocus = "autofocus", #tabindex="2"})
#Html.ValidationMessageFor(m => Model.Manufacturer_Name)
<button tabindex="-1" class="btn-clear" type="button"></button>
</div>
</div>
</div>
<div class="row" style="margin: 0px auto">
<div style="white-space:nowrap" class="span2 readable-text"><label>Pilt:</label> </div>
<div class="span3">
<div class="fallback">
<input type="file" name="DeviceImage" id="DeviceImage" />
</div>
</div>
</div>
</div>
<div class="row" style="margin: 0px auto">
<div class="span10">#Html.ValidationMessage("CustomError")</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="modal-footer">
<div class="grid" style="margin: 0px auto">
<div class="row" style="margin: 0px auto">
<div class="span4"></div>
<div class="span3">
<button class="button large span3" data-dismiss="modal">Cancel</button>
</div>
<div class="span3">
<input class="large span3 success" type="submit" value="Ok" />
</div>
</div>
</div>
</div>
}
Controller:
public ActionResult Create()
{
ViewBag.exsistingDevices = GetExsistingDevices();
return PartialView("_Create");
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin, Super")]
public async Task<ActionResult> Create(Device device, HttpPostedFileBase DeviceImage)
{
try
{
if (ModelState.IsValid)
{
SPDBContext db = new SPDBContext();
device.Last_Action_Date = DateTime.Now;
// Insert image
if (DeviceImage != null)
{
if (DeviceImage.ContentLength > (2 * 1024 * 1024) && DeviceImage.ContentLength < 1)
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
}
var contentType = DeviceImage.ContentType;
device.Image = new byte[DeviceImage.ContentLength];
DeviceImage.InputStream.Read(device.Image, 0, DeviceImage.ContentLength);
}
db.Devices.Add(device);
await db.SaveChangesAsync();
return Json(new { success = true });
}
}
catch (Exception e)
{
ModelState.AddModelError("CustomError", String.Format("{0}: {1}",Resources.Resources.Insert_Failed, e.Message));
}
return PartialView("_Create", device);
}
Model:
[NotMapped]
public HttpPostedFileBase DeviceImage { get; set; }
public byte[] Image { get; set; }

Can not click on controls MVC

I manually placed the following code in the _Layout.cshtml
#model AssignmentTrialMVC.Models.Search
<div id="loginStrip">
#if (Request.IsAuthenticated)
{
<span style="border-right: 1px solid white; padding-right: 5px;">
<strong>#Html.Encode(User.Identity.Name)</strong>
#Html.ActionLink("Sign Out", "Logout", "User")
</span>
}
else
{
<span style="border-right: 1px solid white; padding-right: 5px;">
#Html.ActionLink("Register", "Register", "User")
</span>
<span style="padding-left: 5px;">
#Html.ActionLink("Sign In", "Login", "User")
</span>
}
</div>
<div id="searchCell">
#using (Html.BeginForm())
{
#Html.TextBoxFor(u => u.brand)
<input id="searchSubmitButton" type="submit" value="SEARCH" />
}
</div>
I can view the controls in the screen but I can not click on the controls, but using tab I can focus the controls.
Please help me with this.

Not able to fire button's click in MVC 4 razor?

I am new to MVC, i want to create a login control and for i have write the below code in view:
#using (Html.BeginForm("LoginControl", "Login"))
{
<div style="float: left; line-height: 36px;">
#Html.ValidationSummary()
<div style="float: left; margin-right: 20px;">
UserName :
</div>
<div style="float: right;">#Html.TextBoxFor(o => o.login.UserName)
</div>
<br />
<div style="float: left; margin-right: 20px;">
Password :
</div>
<div style="float: right;">#Html.TextBoxFor(o => o.login.UserPassword)</div>
<br />
<div style="float: left; margin-right: 20px;">
</div>
<input type="button" value="Login" id="Login" title="Login" />
</div>
}
in controller my code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
public class LoginController : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
public ActionResult LoginControl(String UserName, String UserPassword)
{
string result = "";
if (UserName == "ram" && UserPassword == "ram123")
{
result = "1";
}
if (result == "1")
{
return View("LoginControl");
}
else
{
ModelState.AddModelError("", "Invalid username or password");
return View("Index");
}
}
}
}
I am not able to get why it is working, please help me.
ok first of all change the input type of button to submit:
<input type="button" value="Login" id="Login" title="Login" />
Now Change the View Code as below:
#using (Html.BeginForm("LoginControl", "Login"))
{
<div style="float: left; line-height: 36px;">
#Html.ValidationSummary()
<div style="float: left; margin-right: 20px;">
UserName :
</div>
<div style="float: right;">#Html.TextBox("UserName")
</div>
<br />
<div style="float: left; margin-right: 20px;">
Password :
</div>
<div style="float: right;">#Html.TextBox("UserPassword")
</div>
<br />
<div style="float: left; margin-right: 20px;">
</div>
<input type="submit" value="Login" id="Login" title="Login" />
</div>
}
Hope this will help you.

Pass selected value from dropdownlistfor back to controller via Html.BeginForm

I have this code in my view:
<% using (Html.BeginForm("TradeUKKPIShowData", "Report")) //action/controller
{ %>
<div style="padding: 10px; background-color: #eeeeee; width: 300px; border: 1px solid #cccccc;">
<div style="float: left; width: 150px; padding-top: 3px;">
<%: Html.Label("Select a Sunday:") %>
</div>
<div style="float: left; width: 150px;">
<%: Html.DropDownListFor(model => model.SelectedSunday, Model.AllSundays) %>
</div>
<div class="clear"></div>
</div>
<div style="text-align:right; width: 272px; padding-top: 30px;">
<input type="submit" id="search-submit" value="Submit"/>
</div>
<% } %>
The action in the controller:
public ActionResult TradeUKKPIShowData(DateTime date) //this date value is null thus error
{
var reportData = _reportingService.GetTradeUKKPISearches(date);
ViewBag.reportdate = date;
return View(reportData);
}
I get an error stating that the parameter passed, date, is null
where have i gone wrong?
ModelBinding won't work for a parameter named date because its different from the name of your select field. Use the proper name in your action parameter.
public ActionResult TradeUKKPIShowData(DateTime SelectedSunday) {}
You could also bind your ViewModel (don't know the type so i use TypeOfYourViewModel)
public ActionResult TradeUKKPIShowData(TypeOfYourViewModel model)
{
var reportData = _reportingService.GetTradeUKKPISearches(model.SelectedSunday);
ViewBag.reportdate = date;
return View(reportData);
}

Resources