Thanks for replying.i tried this code but got an error i-e
Cannot perform runtime binding on a null reference
Description: An unhandled exception occurred during the execution of the current web request. Please
review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
It's null because you are not passing back a model with the path to your file, you are just rendering the View using View(). Ideally you want a strongly typed model with a property to the file.
However you can also use the ViewBag to send data to the view with the filename. For example:
BushraWaris
Member
2 Points
16 Posts
imageuploader in MVC3
Apr 30, 2012 05:53 PM|LINK
Hey i am new in MVC3.I have to develope an image uploader and jquery type thing in MVC3 as we can do inAsp.net..Can anybody help me??
Slicksim
Participant
1925 Points
350 Posts
Re: imageuploader in MVC3
Apr 30, 2012 08:20 PM|LINK
Hi,
There are many available already.
Try uploadify.
Si
BushraWaris
Member
2 Points
16 Posts
Re: imageuploader in MVC3
May 01, 2012 01:05 PM|LINK
i have done this code.it saves images in Images folder.but now the question is how to display that image i-e src of image=??....
Controller code
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string path = System.IO.Path.Combine(Server.MapPath("~/Images"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
return View();
}
Veiw code
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload Image" />
<br />
}
</p>
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: imageuploader in MVC3
May 01, 2012 04:39 PM|LINK
<img src='@Model.PathOfTheImage' />
BushraWaris
Member
2 Points
16 Posts
Re: imageuploader in MVC3
May 01, 2012 08:25 PM|LINK
Thanks for replying.i tried this code but got an error i-e
Cannot perform runtime binding on a null reference
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
CodeHobo
All-Star
18647 Points
2647 Posts
Re: imageuploader in MVC3
May 01, 2012 08:55 PM|LINK
It's null because you are not passing back a model with the path to your file, you are just rendering the View using View(). Ideally you want a strongly typed model with a property to the file.
However you can also use the ViewBag to send data to the view with the filename. For example:
In your controller
[HttpPost] public ActionResult Index(HttpPostedFileBase file) { string path = System.IO.Path.Combine(Server.MapPath("~/Images"), System.IO.Path.GetFileName(file.FileName)); file.SaveAs(path); ViewBag.Message = "File uploaded successfully"; ViewBag.FileName= file.FileName; return View(); }In your view
Blog | Twitter : @Hattan
ignatandrei
All-Star
134983 Points
21638 Posts
Moderator
MVP
Re: imageuploader in MVC3
May 02, 2012 03:24 AM|LINK
Do you pass back a Model? A path to image into the Model?
Did you tried my code without making any changes to the action?!