Values from my login view also populate fields in my other views - asp.net-mvc

When I login I enter my user name and password.
On a different create view these values also populate the email address and the password fields.
Why is this, and what can I do to stop it?
The next 2 markups are my login view and my create view
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ITOC.WebUI.Models.LogOnModel>" %>
<asp:Content ID="loginTitle" ContentPlaceHolderID="TitleContent" runat="server">
Log On
</asp:Content>
<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Log On</h2>
<p>
Please enter your username and password.
</p>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<%: Html.LabelFor(m => m.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(m => m.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(m => m.Password) %>
<%: Html.ValidationMessageFor(m => m.Password) %>
</div>
<div class="editor-label">
<%: Html.CheckBoxFor(m => m.RememberMe) %>
<%: Html.LabelFor(m => m.RememberMe) %>
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
</asp:Content>
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<ITOC.WebUI.Models.UserExtended>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.Forename) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.Forename)%>
<%: Html.ValidationMessageFor(model => model.User.Forename)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.Surname) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.Surname)%>
<%: Html.ValidationMessageFor(model => model.User.Surname)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.User.EmailAddress) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.User.EmailAddress)%>
<%: Html.ValidationMessageFor(model => model.User.EmailAddress)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.Password)%>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "List") %>
</div>
</asp:Content>

This is probably due to a browser autocompletion feature. You could append the non standard autocomplete="off" to your input fields:
<%: Html.TextBoxFor(model => model.User.Forename, new { autocomplete = "off" }) %>

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.

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.

In MVC, one Edit HttpPost is working, the other one isn't. What am I missing?

Googling generally and on SO hasn't helped me yet, so:
I am building my first MVC application from scratch, going by the MVC Music Store example but instead building a little application where arena Fighters can be created and made to Fight each other. (Fighters and Fight have been made linked to underlying tables through EF).
I have controllers for both the Fighters and the Fights. The Edit Actionresult for Fights is working, but for Fighters it is not. When I hit the button to save my alterations I return to the associated Index page, but no changes have been committed. This is my question: why is this failing?
From BarracksController, with the faulty non-updating HttpPost Edit (should have been named FighterController, but neverthemind):
//
// GET: /Barracks/Edit
public ActionResult Edit(int id)
{
ViewData.Model = _FightDb.Fighters.Single(f => f.Id == id);
return View();
}
//
// POST: /Barracks/Edit
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var fighter = _FightDb.Fighters.Single(f => f.Id == id);
try
{
UpdateModel(fighter, "Fighter");
var x = ViewData.GetModelStateErrors();
_FightDb.SaveChanges();
return RedirectToAction("Index");
}
catch
{
var viewModel = fighter;
return View(viewModel);
}
}
(As you can see, I've included the GetModelStateErrors trick from this SO question, but the result for x is null)
This is the controller that does work, FightController:
//
// GET: /Fights/Edit
public ActionResult Edit(int id)
{
var viewModel = new FightDetailsViewModel
{
Fight = _FightDb.Fights.Single(f => f.ID == id),
Fighters = _FightDb.Fighters.ToList()
};
return View(viewModel);
}
//
// POST: /Fights/Edit
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var fight = _FightDb.Fights.Single(f => f.ID == id);
try
{
UpdateModel(fight, "Fight");
_FightDb.SaveChanges();
return RedirectToAction("Index");
}
catch
{
var viewModel = new FightDetailsViewModel
{
Fight = _FightDb.Fights.Single(f => f.ID == id),
Fighters = _FightDb.Fighters.ToList()
};
return View(viewModel);
}
}
This is edit.aspx for the Fighters: (Edited after comment)
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3_EF_BW_Fight.Models.Fighter>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
<h2>Edit</h2>
<%: Html.EditorForModel() %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
Which uses the following Fighter.ascx in Shared:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fighter>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fighter</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterName) %>
<%: Html.ValidationMessageFor(model => model.FighterName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterStyleDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterLongDescription) %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.FighterLongDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterLongDescription) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
This is the edit.aspx for Fights
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3_EF_BW_Fight.ViewModels.FightDetailsViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
<h2>Edit</h2>
<%: Html.EditorFor(model => model.Fight, new { Fighters = Model.Fighters })%>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
And this is the Fight.ascx:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fight>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.ID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ID) %>
<%: Html.ValidationMessageFor(model => model.ID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FightName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FightName) %>
<%: Html.ValidationMessageFor(model => model.FightName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Fighter1ID) %><br />
<%: Html.LabelFor(model => model.Fighter1Reference.Value.FighterName)%>
</div>
<div class="editor-field">
<%: Html.DropDownList("Fighter1ID", new SelectList(ViewData["Fighters"] as IEnumerable, "ID", "FighterName", Model.Fighter1ID))%>
<%: Html.ValidationMessageFor(model => model.Fighter1ID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Fighter2ID) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Fighter1ID", new SelectList(ViewData["Fighters"] as IEnumerable, "ID", "FighterName", Model.Fighter1ID))%>
<%: Html.ValidationMessageFor(model => model.Fighter2ID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Fighter1Login) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Fighter1Login) %>
<%: Html.ValidationMessageFor(model => model.Fighter1Login) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Fighter2Login) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Fighter2Login) %>
<%: Html.ValidationMessageFor(model => model.Fighter2Login) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FightStatusID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FightStatusID) %>
<%: Html.ValidationMessageFor(model => model.FightStatusID) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
And this is my viewmodel for Fights:
public class FightDetailsViewModel
{
public Fight Fight { get; set; }
public List<Fighter> Fighters { get; set; }
}
There is no ViewModel for Fighters (none that is involved in this scenario, anyway).
I can post any code you may wish to see.
Edit: I've looked at Using ViewModel Pattern with MVC 2 Strongly Typed HTML Helpers and ASP.NET MVC 2 UpdateModel() is not updating values in memory or database , but i haven't seen a solution there yet.
Instead of this UpdateModel(fighter, "Fighter"); try calling the updte model just like this UpdateModel(fighter);. The difference is between the two edits that in case of Fighter your model is directly the Fighter so you do not need the name, while in case of the Fight you call the Editor for model.Fight so you need the name. See this question as well: asp.net mvc2 - how to get model and model.something in the same way in controller?

Resources