This example demonstrates how to bind UploadControl (its posted files) to a Model
property.
C#public ActionResult Index(UserModel modelDTO) {
string fileName = string.Empty;
if (ModelState.IsValid) {
if (modelDTO.Attachment.Length > 0 && modelDTO.Attachment[0].ContentLength > 0) {
fileName = string.Format("~/Content/Files/{0}", modelDTO.Attachment[0].FileName);
modelDTO.Attachment[0].SaveAs(Server.MapPath(fileName));
}
}
SavedModel model = new SavedModel();
model.UserName = modelDTO.UserName;
model.FileUrl = fileName;
return View("Complete", model);
}
Files to Review
- HomeController.cs (VB: HomeController.vb)
- Complete.cshtml (VB: Complete.vbhtml)
- Index.cshtml (VB: Index.vbhtml)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Web;
using DevExpress.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using T983248_MVC.Models;
namespace T983248_MVC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new UserModel());
}
[HttpPost]
public ActionResult Index(UserModel modelDTO)
{
string fileName = string.Empty;
if (ModelState.IsValid)
{
if (modelDTO.Attachment.Length > 0 && modelDTO.Attachment[0].ContentLength > 0)
{
fileName = string.Format("~/Content/Files/{0}", modelDTO.Attachment[0].FileName);
modelDTO.Attachment[0].SaveAs(Server.MapPath(fileName));
}
}
SavedModel model = new SavedModel();
model.UserName = modelDTO.UserName;
model.FileUrl = fileName;
return View("Complete", model);
}
}
}
Visual BasicPublic Class HomeController
Inherits System.Web.Mvc.Controller
Public Function Index() As ActionResult
Return View(New UserModel())
End Function
<HttpPost>
Public Function Index(ByVal modelDTO As UserModel) As ActionResult
Dim fileName As String = String.Empty
If ModelState.IsValid Then
If modelDTO.Attachment.Length > 0 AndAlso modelDTO.Attachment(0).ContentLength > 0 Then
fileName = String.Format("~/Content/Files/{0}", modelDTO.Attachment(0).FileName)
modelDTO.Attachment(0).SaveAs(Server.MapPath(fileName))
End If
End If
Dim model As SavedModel = New SavedModel()
model.UserName = modelDTO.UserName
model.FileUrl = fileName
Return View("Complete", model)
End Function
End Class
Razor@model T983248_MVC.Models.SavedModel
@{
ViewBag.Title = "Complete";
}
<h2>Complete</h2>
<div>
<h4>Result</h4>
@Html.DisplayNameFor(m=>m.UserName):
@Html.DisplayFor(model => model.UserName)
<br/>
<img src="@Url.Content(Model.FileUrl)" style="width:300px;height:300px;" />
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
Code@ModelType T983248_VB.SavedModel
@Code
ViewData("Title") = "Complete"
End Code
<h2>Complete</h2>
<div>
<h4>Result</h4>
@Html.DisplayNameFor(Function(model) model.UserName)
@Html.DisplayFor(Function(model) model.UserName)
<img src="@Url.Content(Model.FileUrl)" style="width:300px;height:300px;" />
</div>
<p>
@Html.ActionLink("Back to List", "Index")
</p>
Razor@model T983248_MVC.Models.UserModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm()) {
@Html.DevExpress().FormLayout(settings => {
settings.Name = "FormLayout";
settings.Items.Add(m => m.UserName);
settings.Items.Add(m => m.Attachment, i => {
i.NestedExtension().UploadControl(s => {
s.ValidationSettings.ShowErrors = true;
});
});
settings.Items.Add(i => {
i.Name = "btnSubmit";
i.ShowCaption = DefaultBoolean.False;
i.NestedExtension().Button(s => {
s.Text = "Submit Invoice";
s.UseSubmitBehavior = true;
});
});
}).GetHtml()
}
Code@ModelType T983248_VB.UserModel
@Code
ViewData("Title") = "Home Page"
End Code
@Using (Html.BeginForm())
@Html.DevExpress().FormLayout(Sub(settings)
settings.Name = "FormLayout"
settings.Items.Add(Function(m) m.UserName)
settings.Items.Add(Function(m) m.Attachment, Sub(i)
i.NestedExtension().UploadControl(Sub(s) s.ValidationSettings.ShowErrors = True)
End Sub)
settings.Items.Add(Sub(i)
i.Name = "btnSubmit"
i.ShowCaption = DefaultBoolean.[False]
i.NestedExtension().Button(Sub(s)
s.Text = "Submit Invoice"
s.UseSubmitBehavior = True
End Sub)
End Sub)
End Sub).GetHtml()
End Using