Upload with Strongly Typed View - Problem - asp.net-mvc

I'm have a problem with Upload with strongly typed view in ASP.NET MVC.
The value of input (view) is coming null for the view. My code follow:
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Create", "DownloadsSub", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Informações do download</legend>
<div class="editor-label">
Selecione a categoria do download
</div>
<div class="editor-field">
<%: Html.DropDownList("IDCategoriaDownloads", (IEnumerable<SelectListItem>)ViewData["IDCategoriaDownloads"], "Categorias de downloads...")%>
<%: Html.ValidationMessageFor(model => model.IDCategoriaDownloads)%>
</div>
<div class="editor-label">
Selecione o formato do arquivo
</div>
<div class="editor-field">
<%= Html.DropDownList("IDFormatoArquivo", (IEnumerable<SelectListItem>)ViewData["IDFormatoArquivo"], "Formatos de arquivos...") %>
<%: Html.ValidationMessageFor(model => model.IDFormatoArquivo)%>
</div>
<div class="editor-label">
Título do download
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.TituloDownload)%>
<%: Html.ValidationMessageFor(model => model.TituloDownload)%>
</div>
<div class="editor-label">
Descrição do download
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.DescricaoDownload)%>
<%: Html.ValidationMessageFor(model => model.DescricaoDownload)%>
</div>
<div class="editor-label">
Data de Postagem
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DataDownload)%>
<%: Html.ValidationMessageFor(model => model.DataDownload)%>
</div>
<div class="editor-label">
Selecione o arquivo para download
</div>
<div class="editor-field">
<input type="file" id="txtFile" name="txtFile" />
<%--<%: Html.TextBoxFor(model => model.CaminhoDownload) %>
<%: Html.ValidationMessageFor(model => model.CaminhoDownload)%>--%>
</div>
<div class="editor-label">
Quantidade inicial de cliques
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.HitsDownload)%>
<%: Html.ValidationMessageFor(model => model.HitsDownload)%>
</div>
<div class="editor-label">
Qual o tamanho do arquivo em bytes?
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.TamanhoDownload) %>
<%: Html.ValidationMessageFor(model => model.TamanhoDownload) %>
</div>
<div class="editor-label">
Possui direitos autorais?
</div>
<div class="editor-field">
<%= Html.DropDownList("StatusDireitosAutorais", (IEnumerable<SelectListItem>)ViewData["StatusDireitosAutorais"], "Selecione...")%>
<%: Html.ValidationMessageFor(model => model.StatusDireitosAutorais)%>
</div>
<div class="editor-label">
Direitos autorais (preencha apenas se houver)
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.CreditosDownload)%>
<%: Html.ValidationMessageFor(model => model.CreditosDownload)%>
</div>
<p>
<input type="submit" value="Salvar" />
</p>
</fieldset>
<% } %>
And the controller
[HttpPost]
public ActionResult Create(tbDownloads _novoDownload, HttpPostedFileBase arquivoUp)
{
//var arquivoUp = this.Request.Files[0];
string nomeArquivoSalvo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FilesUpload", "Downloads");
nomeArquivoSalvo = Path.Combine(nomeArquivoSalvo, Path.GetFileName(arquivoUp.FileName));
arquivoUp.SaveAs(Server.MapPath("~/FilesUpload/Downloads/") + nomeArquivoSalvo);
_novoDownload.CaminhoDownload = nomeArquivoSalvo.ToString();
if (ModelState.IsValid)
{
modelo.AddTotbDownloads(_novoDownload);
modelo.SaveChanges();
return RedirectToAction("Sucesso", "Mensagens");
}
return View(_novoDownload);
}
Ideas?
Thanks for help!

The name of the HttpPostedFileBase property needs to match the name of the input field in the view.
In the view, you have called it "txtFile":
<div class="editor-field">
<input type="file" id="txtFile" name="txtFile" />
<%--<%: Html.TextBoxFor(model => model.CaminhoDownload) %>
<%: Html.ValidationMessageFor(model => model.CaminhoDownload)%>--%>
</div>
But in the controller you refer to it as:
[HttpPost]
public ActionResult Create(tbDownloads _novoDownload,
HttpPostedFileBase arquivoUp)
{ [...] }
If you change this to:
[HttpPost]
public ActionResult Create(tbDownloads _novoDownload,
HttpPostedFileBase txtFile)
{ [...] }
It should all work.

Related

how to simultaneously pass A model as well as a File to the controller?

i have a reports model in my MVC project. Now i have to send a file to the controller simultaneously. I am using HttpPostedFileBase as parameter with my reports model.
Reports Model is :
Public Class ReportsModel
Public Sub New()
Authentication = "private"
End Sub
Private UploadDate_ As String
<Display(Name:="ID")> _
Public Property id As UInteger
<Display(Name:="Serial Number")> _
Public Property Srno As UInteger
<Display(Name:="User Name")> _
Public Property UserName As String
<Display(Name:="Details")> _
Public Property Details As String
End Class
And My Controller is :
<HttpPost()> _
<AllowAnonymous()> _
<ValidateAntiForgeryToken()> _
Public Function EditReport(ByVal mdl As ReportsModel, ByVal FileUpl As HttpPostedFileBase ) As ActionResult
Return View(mdl)
End Function
Here is my View:
<% Using Html.BeginForm() %>
<%: Html.AntiForgeryToken() %>
<%: Html.ValidationSummary(True) %>
<fieldset>
<legend>ReportsModel</legend>
<%: Html.HiddenFor(Function(model) model.id) %>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Srno) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.Srno) %>
<%: Html.ValidationMessageFor(Function(model) model.Srno) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.UserName) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.UserName) %>
<%: Html.ValidationMessageFor(Function(model) model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Details) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.Details) %>
<%: Html.ValidationMessageFor(Function(model) model.Details) %>
</div>
<%--This Code Below is running correctly on passing it , But not with Reports Model--%>
<form action="/phed/EditReport" method="post" enctype="multipart/form-data">
<label for="FileUpl1">Upload File: </label>
<input type="file" name="FileUpl" id="FileUpl" />
<span style="color: red;"><%:TempData("Message")%></span>
<p>
<input type="submit" value="Save" />
</p>
</form>
</fieldset>
<% End Using %>
How to send this FileUpl along with ReportsModel to the controller ? I Can Not change reports Model.
I got it. I have Change the form action and enctype to "multipart/form-data". Like:
<div>
<form action="/phed/EditReport" method="post" enctype="multipart/form-data">
<%: Html.AntiForgeryToken() %>
<%: Html.ValidationSummary(True) %>
<fieldset>
<legend>ReportsModel</legend>
<%: Html.HiddenFor(Function(model) model.id) %>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Details) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.Details) %>
<%: Html.ValidationMessageFor(Function(model) model.Details) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.BillsOfMonth) %>
</div>
<div class="editor-field">
<%: Html.TextBox("BillsOfMonth", Model.BillsOfMonth, New With {Key .class = "monthpicker"})%>
<%: Html.ValidationMessageFor(Function(model) model.BillsOfMonth) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.UplDate) %>
</div>
<div class="editor-field">
<%: Html.TextBox("UplDate", Model.UplDate, New With {Key .class = "datepicker"})%>
<%: Html.ValidationMessageFor(Function(model) model.UplDate) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Authentication) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Authentication", New List(Of SelectListItem) From { _
New SelectListItem With {.Text = "public", .Value = "public"}, _
New SelectListItem With {.Text = "private", .Value = "private"}})%>
<%: Html.ValidationMessageFor(Function(model) model.Authentication) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Download) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.Download, New With {Key .readonly = "readonly"})%>
<%: Html.ValidationMessageFor(Function(model) model.Download) %>
</div>
<label for="FileUpl1">Upload File: </label>
<input type="file" name="FileUpl" id="FileUpl" />
<span style="color: red;"><%:TempData("Message")%></span>
<input type="submit" class="sort-submit" value="Upload File" />
</fieldset>
</form>
</div>
It seems you create a form nested to another form which is not allowed in HTML, so if you want to upload your file, you have to remove the second Form. The BeginForm method renders a form that will be handled by a controller action method.
You can use this method in a using block. In that case, the method renders the closing tag at the end of the using block.
And you can do like this:
<% Using Html.BeginForm("EditReport","phed",Nothing,FormMethod.Post, New With {.enctype="multipart/form-data"}) %>
<%: Html.AntiForgeryToken() %>
<%: Html.ValidationSummary(True) %>
<fieldset>
<legend>ReportsModel</legend>
<%: Html.HiddenFor(Function(model) model.id) %>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Srno) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.Srno) %>
<%: Html.ValidationMessageFor(Function(model) model.Srno) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.UserName) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.UserName) %>
<%: Html.ValidationMessageFor(Function(model) model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Details) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(Function(model) model.Details) %>
<%: Html.ValidationMessageFor(Function(model) model.Details) %>
</div>
<%--This Code Below is running correctly on passing it , But not with Reports Model--%>
<label for="FileUpl1">Upload File: </label>
<input type="file" name="FileUpl" id="FileUpl" />
<span style="color: red;"><%:TempData("Message")%></span>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% End Using %>
I hope it will help.

how to bind data to label in asp.net mvc3

I have three model Classes(code first)
ProductGroup(p)->product(c)(list of produGroups comes in drop down list while creating a new product),
Now my requirement is,
Product(p)->PreferedLocation(c),
when I select preferedLocation(Create action) link ,I have to fill the details for the particular product.Means first I need to display single product name in Label like Product:Mango
In controller
public ActionResult PreferedLocation()
{
ViewBag.ProductID = new SelectList(db.Products,"ProductID","Name");
//ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "Name");
return View();
}
//
// POST: /PreferedLocation/Create
[HttpPost]
public ActionResult PreferedLocation(PreferredLocation preferredlocation)
{
if (ModelState.IsValid)
{
db.PreferredLocations.Add(preferredlocation);
db.SaveChanges();
return RedirectToAction("Index");
}
// ViewBag.ProductID = new SelectList(db.Products, "ProductID", "Name", preferredlocation.ProductID);
return View(preferredlocation);
}
In View:
<div class="editor-field">
<%: Html.EditorFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.StreetAdress) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.StreetAdress) %>
<%: Html.ValidationMessageFor(model => model.StreetAdress) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Latitude) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Latitude) %>
<%: Html.ValidationMessageFor(model => model.Latitude) %>
</div>
<div class="display-label">product</div>
<div class="display-field">
<%: Html.LabelFor(model=>Model.Product.Name) %>
<%: Html.ValidationMessageFor(model => model.ProductID) %>
</div>
Here I tried Code for idsplaying product name in label
but instead of product name I got output as
product
Product Name.
What modifications I have to do.
Use DisplayFor instead of LabelFor.

ASP.NET MVC DropDownList and HTML Attributes

I am having a dropdownlist in my application in edit form.
But somewhere I was wrong.
HomeController.cs
ViewData["RealizatorID"] = from r in db.tGlavnas
select new SelectListItem { Text = r.Realizator.ImeRealizatora,
Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
Edit.aspx
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("RealizatorID")%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
When recording changes in Edit.aspx page report following error to me:
There is no ViewData item of type 'IEnumerable' that has the key 'RealizatorID'.
I suggest you to do the following:
Controller:
ViewData["SelRealizator"] = from r in db.tGlavnas
select new SelectListItem { Text = r.Realizator.ImeRealizatora,
Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
View:
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.RealizatorID, ViewData["SelRealizator"] as IEnumerable<SelectListItem>)%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
Full code:
public ActionResult Edit(int id)
{
var log = db.tGlavnas.Single(lo => lo.RedniBroj == id);
ViewData["SelRealizator"] = from r in db.tGlavnas select new SelectListItem { Text = r.Realizator.ImeRealizatora, Value = r.Realizator.ImeRealizatora, Selected = (r.RealizatorID == id) };
return View(log);
}
I don't write Post method yet.
Edit.aspx
<h2>Edit</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.RedniBroj) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.RedniBroj) %>
<%: Html.ValidationMessageFor(model => model.RedniBroj) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumPrijema) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumPrijema) %>
<%: Html.ValidationMessageFor(model => model.DatumPrijema) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumPredaje) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumPredaje) %>
<%: Html.ValidationMessageFor(model => model.DatumPredaje) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.BrojID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.BrojID) %>
<%: Html.ValidationMessageFor(model => model.BrojID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PredmetJavneNabavke) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PredmetJavneNabavke) %>
<%: Html.ValidationMessageFor(model => model.PredmetJavneNabavke) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.VrstaID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.VrstaID) %>
<%: Html.ValidationMessageFor(model => model.VrstaID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.TipSredstvaID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.TipSredstvaID) %>
<%: Html.ValidationMessageFor(model => model.TipSredstvaID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ProcenjenaVrednost) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ProcenjenaVrednost) %>
<%: Html.ValidationMessageFor(model => model.ProcenjenaVrednost) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PoKontuID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PoKontuID) %>
<%: Html.ValidationMessageFor(model => model.PoKontuID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.RealizatorID) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.RealizatorID, ViewData["SelRealizator"] as
IEnumerable<SelectListItem>)%>
<%: Html.ValidationMessageFor(model => model.RealizatorID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.PonudjacID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.PonudjacID) %>
<%: Html.ValidationMessageFor(model => model.PonudjacID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Iznos) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Iznos, String.Format("{0:F}", Model.Iznos)) %>
<%: Html.ValidationMessageFor(model => model.Iznos) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.BrojRacuna) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.BrojRacuna) %>
<%: Html.ValidationMessageFor(model => model.BrojRacuna) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DatumIsplate) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DatumIsplate, String.Format("{0:g}", Model.DatumIsplate)) %>
<%: Html.ValidationMessageFor(model => model.DatumIsplate) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.JavnaNabSprovPostID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.JavnaNabSprovPostID) %>
<%: Html.ValidationMessageFor(model => model.JavnaNabSprovPostID) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>

Asp MVC partial does not validated

Scenario :
Viewmodel dienstViewModel contains a AdresViewModel
Public Class AdresViewModel
<Required(ErrorMessage:="Gelieve een straatnaam op te geven")>
<DisplayName("Straat:")>
Property Straat As String
<Required(ErrorMessage:="Gelieve een huisnummer op te geven")>
<DisplayName("Huisnummer:")>
Property HuisNummer As String
<Required(ErrorMessage:="Gelieve een gemeente op te geven")>
<DisplayName("Gemeente:")>
<RegularExpression("\b[a-zA-Z0-9._%+-]+,\s[0-9]{4}", ErrorMessage:="Selecteer de correcte gemeente")>
Property Gemeente As String
<DisplayName("Bus")>
Property Bus As Integer
End Class
The view that contains the partial:
<% Using Html.BeginForm()%>
<%: Html.ValidationSummary(True) %>
<fieldset>
<legend>Vervolledig het onderstaand formulier:</legend>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.DienstNaam) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.DienstNaam) %>
<%: Html.ValidationMessageFor(Function(model) model.DienstNaam) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.DienstOmschrijving) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.DienstOmschrijving) %>
<%: Html.ValidationMessageFor(Function(model) model.DienstOmschrijving) %>
</div>
</fieldset>
<fieldset>
<legend>Adres gegevens</legend>
<% Html.RenderPartial("Adres", New ViewDataDictionary(Model.DienstAdres))%>
</fieldset><p>
<input type="submit" value="Create" />
</p>
<% End Using %>
When i press the commit button on the end only the first 2 textboxes get validated.
How do i make sure that the partial view also gets validated for correct input?
Or are partials only used to show information and not to retrieve information?
Partial view
<%# Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of Anip.WebGUI.ViewModels.AdresViewModel)" %>
<%-- The following line works around an ASP.NET compiler warning --%>
<%: ""%>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Straat)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.Straat)%>
<%: Html.ValidationMessageFor(Function(model) model.Straat)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.HuisNummer)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.HuisNummer)%>
<%: Html.ValidationMessageFor(Function(model) model.HuisNummer)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Bus)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.Bus)%>
<%: Html.ValidationMessageFor(Function(model) model.Bus)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(Function(model) model.Gemeente)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(Function(model) model.Gemeente)%>
<%: Html.ValidationMessageFor(Function(model) model.Gemeente)%>
</div>
Controller Methods that calls the views
'
' GET: /Dienst/Create
Function Create() As ActionResult
Return View(New DienstViewModel())
End Function
'
' POST: /Dienst/Create
<HttpPost()> _
Function Create(ByVal viewModel As DienstViewModel) As ActionResult
If ModelState.IsValid Then
Try
' TODO: Add insert logic here
Return RedirectToAction("Index")
Catch
Return View(viewModel)
End Try
Else
Return View(viewModel)
End If
probably you are not parsing your POST result into an object of the AdresViewModel, when the POST action is called.
can you copy the code of your action?
for example: (C#)
public ActionResult Edit(AdresViewModel mod) {
}
Edit:
you did:
<% Html.RenderPartial("Adres", New ViewDataDictionary(Model.DienstAdres))%>
but it should be:
<% Html.RenderPartial("Adres", Model.DienstAdres, new ViewDataDictionary()); %>

passing values from view to Controller N then to other View

Customer:
<p></p>
<p></p>
<div> <% foreach (var item in Model)
{ %>
You are viewing Users of Customer: <%:item.Customer %>
<%break; %>
<%} %></div>
<p></p>
<% Html.RenderPartial("EditUsers", Model); %>
<p>
<%: Html.ActionLink("Create New", "Create", "Profile", null, null)%>
</p>
Now i got to pass the <%:item.Customer %> through the %: Html.ActionLink("Create New", "Create", "Profile", null, null)%> and that should be displayed in the Create view
here is the Controller n i will give the Create view too
Controller:
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(string UserName, string Password, string FirstName, string LastName,
string MiddleInitial, string Email,string Telephone, bool IsAdmin, bool IsSubAdmin)
{
UserDAL userDALObject = new UserDAL();
tblUser newUser = new tblUser();
newUser.Customer = customerNumber;
newUser.UserName = UserName;
newUser.Password = Password;
newUser.FirstName = FirstName;
newUser.LastName = LastName;
newUser.MiddleInitial = MiddleInitial;
newUser.Email = Email;
newUser.Telephone = Telephone;
newUser.IsAdmin = IsAdmin;
newUser.IsSubAdmin = IsSubAdmin;
userDALObject.AddUserDetails(newUser);
TempData["UserCreationMsg"] = string.Format("User named :{0}, is created",UserName);
return View();
}
public ActionResult EditUser(string id)
{
UserDAL userDALObject = new UserDAL();
tblUser userDetails = userDALObject.GetUser(Int32.Parse(id));
TempData["EditUserId"] = id;
return View(userDetails);
}
the Create View
<%if(TempData["UserCreationMsg"] != null)%>
<%{ %>
<%: TempData["UserCreationMsg"].ToString() %>
<%} %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<div class="editor-label">
<%: Html.LabelFor(model => model.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBox("UserName") %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.Password("Password") %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FirstName) %>
</div>
<div class="editor-field">
<%: Html.TextBox("FirstName") %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.LastName) %>
</div>
<div class="editor-field">
<%: Html.TextBox("LastName") %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.MiddleInitial) %>
</div>
<div class="editor-field">
<%: Html.TextBox("MiddleInitial") %>
<%: Html.ValidationMessageFor(model => model.MiddleInitial) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBox("Email") %>
<%: Html.ValidationMessageFor(model => model.Email) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Telephone) %>
</div>
<div class="editor-field">
<%: Html.TextBox("Telephone") %>
<%: Html.ValidationMessageFor(model => model.Telephone) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.IsAdmin) %>
</div>
<div class="editor-field">
<%: Html.CheckBox("IsAdmin") %>
<%: Html.ValidationMessageFor(model => model.IsAdmin) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.IsSubAdmin) %>
</div>
<div class="editor-field">
<%: Html.CheckBox("IsSubAdmin") %>
<%: Html.ValidationMessageFor(model => model.IsSubAdmin) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
What exactly is your issue?
On the side point, you should be using Model Binding to pass a person object to the CreateView action method rather than passing each property separately. It will reduce a lot of your code. Search Model Binding for more details.

Resources