Store an image to a database using Entity Framework - asp.net-mvc

I would like to save an image (from file, png or jpg) to the database using Entity Framework.
My code is here:
[HttpPost]
public ActionResult Create(Food fd)
{
try
{
FoodOrderEntities fo = new FoodOrderEntities();
Stream inpStream = Request.InputStream;
Int32 length = Convert.ToInt32(inpStream.Length);
byte[] tempImage = new byte[length];
inpStream.Read(tempImage, 0, length);
//FoodImage is an image field in the datatable, mapped by the Entity Framework
fd.FoodImage = tempImage;
fo.AddToFood(fd);
fo.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
My view is:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% using (Html.BeginForm("Create","Food",FormMethod.Post,new {enctype="multipart/form-data"})) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.FoodName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FoodName) %>
<%: Html.ValidationMessageFor(model => model.FoodName) %>
</div>
<div>
Select a file: <input type="file" name="fileUpload" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
But, using Request.Files["fileUpload"] I got only null. Then, I have used this code, but I could get only 124 bytes from my image. Instead inpStream.Length I have used Request.TotalBytes and Request.ContentLength, but with the same result. Please, help me! Thank you in advance.
Best regards,
alenan13

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 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()); %>

The wrong of two ValidationSummaries gets updated (inside of a partial view)

In my little app I've a strongly typed partial view (the Login Form) this is rendered within the Masterpage, this looks as follows:
<!-- Login Box -->
<div class="login">
<div class="login-bg clearingfix">
<% Html.RenderPartial("LoginViewControl", Model); %>
</div>
</div>
<!-- / Login Box -->
The Model of this partial view is:
using System;
using System.Web;
using System.ComponentModel.DataAnnotations;
using mpws.Util.Validation;
namespace mpws.Models
{
public class LoginViewModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
}
}
And the View itself contains a Validationsummary: <%= Html.ValidationSummary() %>
I created another View called SignUp, also this is strongly typed with the type as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using mpws.Util.Validation;
namespace mpws.Models
{
public class SignUpViewModel
{
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string EmailAddress { get; set; }
[Required]
public string EmailAddressRepeat { get; set; }
public string Occupation { get; set; }
public string SuggestedBy { get; set; }
public IValidationState validationState { get; set; }
}
}
here my SignUp View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mpws.Models.SignUpViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
SignUp
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>SignUp</h2>
<div id="SignUpForm">
<% using (Html.BeginForm("SignUp","Account", new mpws.Models.SignUpViewModel())) {%>
<%: Html.ValidationSummary() %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.Resource("Strings, Username") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName) %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, Password") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Password) %>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, FirstName") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, LastName") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LastName) %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, EmailAddress") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.EmailAddress) %>
<%: Html.ValidationMessageFor(model => model.EmailAddress) %>
</div>
<div class="editor-label">
<%: Html.Resource("RepeatEmailAddress") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.EmailAddressRepeat) %>
<%: Html.ValidationMessageFor(model => model.EmailAddressRepeat) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Occupation) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Occupation) %>
<%: Html.ValidationMessageFor(model => model.Occupation) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.SuggestedBy) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.SuggestedBy) %>
<%: Html.ValidationMessageFor(model => model.SuggestedBy) %>
</div>
<p>
<button class="button positive" type="submit">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" />
<%: Html.Resource("Strings, SignUp") %>
</button>
<button class="button negative" type="reset">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/cross.png") %>" />
<%: Html.Resource("Strings, Cancel") %>
</button>
</p>
</fieldset>
<% } %>
</div>
</asp:Content>
and the login partial View:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<mpws.Models.LoginViewModel>" %>
<!-- This should be outsourced into it own .js fila -->
<script type="text/javascript">
/// <reference path="jquery-1.4.1-vsdoc.js" />
$(document).ready(function () {
$('#LoginForm form').live('submit', function () {
$.post($(this).attr('action'), $(this).serialize(), function (result) {
if ($.isString(result)) {
$("#LoginForm").replaceWith($(result));
}
else if (result["Successfull"]) {
window.location.href = result["RedirectUrl"];
}
else {
alert('Unknown Error...');
}
});
return false;
});
});
jQuery.isString = function (o) {
return (typeof o === "string");
}
</script>
<div id="LoginForm">
<% using (Html.BeginForm("SignIn", "Account"))
{%>
<fieldset>
<legend>Login</legend>
<%if(!ViewContext.ViewData.ModelState.IsValid) {%>
<%= Html.ValidationSummary_jQueyUIfriendly() %>
<%= Html.ValidationSummary() %>
<br />
<%} %>
<div class="editor-label">
<%: Html.Resource("Strings, Username") %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName)%> <br />
<%: Html.ValidationMessageFor(model => model.UserName)%>
</div>
<div class="editor-label">
<%: Html.Resource("Strings, Password") %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.Password)%><br />
<%: Html.ValidationMessageFor(model => model.Password)%>
</div>
<p>
<button class="button positive" type="submit">
<img alt="" src="<%= ResolveUrl("~/Content/public/ui/button/tick.png") %>" />
<%: Html.Resource("Strings, SignIn") %>
</button>
</p>
</fieldset>
<% } %>
</div>
Well if now someone clicks the "SignUp"-Submitbutton, the validation errors are shown in the validationsummary of the signUp view but also in the validationsummary of the partial view "LoginViewControl"... Any idea why? I thought maybe the Modelstate is globally handeld but if I click the "login"-Submitbutton the validation Messages are only shown in the partial view...
Any idea?
Thanks in advance
Johannes

Values from my login view also populate fields in my other views

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" }) %>

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