I gotta the following code in controller and view. The problem is that the model(Photo is an Entity Framework entity) is empty(all fields are nulled). Why?
// GET: /Admin/Photo/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Admin/Photo/Create
[HttpPost]
public ActionResult Create(int id, FormCollection collection)
{
try
{
var file = (HttpPostedFileBase) Request.Files[0];
if (file != null && file.FileName != null)
{
var filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Photos/Product/", Path.GetFileName(file.FileName));
file.SaveAs(filename);
var photo = new Photo();
photo.Description = collection["Description"];
photo.Main = collection["Main"].Contains("true");
photo.Filename = Path.GetFileName(file.FileName);
photo.Product_Id = id;
Entities.AddToPhotos(photo);
Entities.SaveChanges();
}
else
{
ModelState.AddModelError("", "Plik musi zostać załadowany.");
return View();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
<h2>Create</h2>
<% using (Html.BeginForm(null, null, null, FormMethod.Post, new {enctype = "multipart/form-data" })) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Description) %>
<%: Html.ValidationMessageFor(model => model.Description) %>
</div>
<div class="editor-label">
<label for="MainContent_file">Plik: </label>
</div>
<div class="editor-field">
<asp:FileUpload ID="file" runat="server" />
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Main) %>
</div>
<div class="editor-field">
<%: Html.CheckBoxFor(model => model.Main) %>
<%: Html.ValidationMessageFor(model => model.Main) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
Update: I checked and the collection is populated with proper fields, but they are all nulled.
Check to make sure that the name attributes in the resulting html match your collection name. You could also change your public ActionResult Create(int id, FormCollection collection) to public ActionResult Create(int id, YourViewModel model) to automagically map the post values to the model.
Check the html source in the browser.
It might be sending them as: "Photo.Description"
Related
I've got two DropDownLists in a form on a page that contain the same values (a list of languages). I want to ensure that the user doesn't select the same value in each of the drop downs.
I've tried using JavaScript to ensure the selected values aren't the same, it works fine but the form submits anyway.
What's the best way to accomplish this?
Here's the code from my View:
<script type="text/javascript">
function CheckLanguageDDL()
{
var form = document.getElementById("form0");
var sourceLangIndex = form.SourceLanguage.selectedIndex;
var targetLangIndex = form.TargetLanguage.selectedIndex;
var strSourceLanguage = form.SourceLanguage.options[sourceLangIndex].text;
var strTargetLanguage = form.TargetLanguage.options[targetLangIndex].text;
if (strSourceLanguage == strTargetLanguage)
{
alert("Source Language and Target Language must be different!");
return;
}
}
</script>
<% Html.BeginForm("Index", "Translate", FormMethod.Post, new { enctype = "multipart/form-data" }); %>
<fieldset>
<legend>Request</legend>
<br />
<div class="editor-label">
<%: Html.LabelFor(m => m.SourceLanguage) %>:
</div>
<div class="editor-field">
<%: Html.DropDownList("SourceLanguage", (IEnumerable<SelectListItem>)ViewData["SourceLanguages"]) %>
<%: Html.ValidationMessageFor(m => m.SourceLanguage) %>
</div>
<br />
<div class="editor-label">
<%: Html.LabelFor(m => m.TargetLanguage) %>:
</div>
<div class="editor-field">
<%: Html.DropDownList("TargetLanguage", (IEnumerable<SelectListItem>)ViewData["TargetLanguages"]) %>
<%: Html.ValidationMessageFor(m => m.TargetLanguage) %>
</div>
<input type="submit" value="Submit Request" onclick="CheckLanguageDDL();" />
</p>
</fieldset>
Thanks.
Make the function return a true/false value that the form submit use that return value
function CheckLanguageDDL()
{
var form = document.getElementById("form0");
var sourceLangIndex = form.SourceLanguage.selectedIndex;
var targetLangIndex = form.TargetLanguage.selectedIndex;
var strSourceLanguage = form.SourceLanguage.options[sourceLangIndex].text;
var strTargetLanguage = form.TargetLanguage.options[targetLangIndex].text;
if (strSourceLanguage == strTargetLanguage)
{
return false;
}
return true;
}
And on the button:
onclick="return CheckLanguageDDL();"
When I try to post this email : "name+1#domain.com" I get "name 1#domain.com" in my Model's property in the controller. Anyone know what I can do so the '+' will not disappear?
Thanks
Edited :
Ok I think I did not give enough explication :
I have a form that allow my users to search for members by different properties, one of them is Email. In the current system I have members that the email is using '+' inside the string. (like this : name+1#domain.com) When I try to search for this user using his email, the value that is posted to my controller does not contains the '+' so it results as 'name 1#domain.com' and the search do not return anything.
I'm pretty sure it's something about the encoding but I don't use any link here, I post a full SearModel witch contains a string property named "Email" witch is automatically bind to my Textbox with the same name in my view.
Can a Specify to encode a property of a Model object?
Thanks again!
Here's my Model :
public class SearchMemberModel
{
[LocalizedDisplayName("MemberNumber")]
public string MemberNumber { get; set; }
[LocalizedDisplayName("Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[LocalizedDisplayName("FirstName")]
public string FirstName { get; set; }
[LocalizedDisplayName("LastName")]
public string LastName { get; set; }
[LocalizedDisplayName("Phone")]
public string Phone { get; set; }
[LocalizedDisplayName("ActiveOnly")]
public bool Active { get; set; }
}
And my controller's action :
public virtual ActionResult ListAjax(SearchMemberModel search)
{
var gridModel = new GridModel<SerializableMember>();
var data = _session.All<Member>();
if (search != null)
{
if (search.Active) data = data.Where(x => x.Active);
if (!string.IsNullOrEmpty(search.Email)) data = data.Where(x => x.Email.Contains(search.Email));
if (!string.IsNullOrEmpty(search.FirstName)) data = data.Where(x => x.FirstName.Contains(search.FirstName));
if (!string.IsNullOrEmpty(search.LastName)) data = data.Where(x => x.LastName.Contains(search.LastName));
if (!string.IsNullOrEmpty(search.MemberNumber)) data = data.Where(x => x.MemberNumber.Contains(search.MemberNumber));
if (!string.IsNullOrEmpty(search.Phone)) data = data.Where(x => x.Phone.Contains(search.Phone));
}
var list = new List<SerializableMember>(data.Count());
list.AddRange(data.ToList().Select(obj => new SerializableMember(obj)));
gridModel.Data = list;
return View(gridModel);
}
And the view (taught it was not necessary) :
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<Enquete.Models.SearchMemberModel>" %>
<%:Resources.TitleMemberList %>
<%:Resources.TitleMemberList %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend><%: Resources.Search %></legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.MemberNumber) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.MemberNumber) %>
<%: Html.ValidationMessageFor(model => model.MemberNumber) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Email) %>
<%: Html.ValidationMessageFor(model => model.Email) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FirstName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FirstName) %>
<%: Html.ValidationMessageFor(model => model.FirstName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.LastName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.LastName) %>
<%: Html.ValidationMessageFor(model => model.LastName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Phone) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Phone) %>
<%: Html.ValidationMessageFor(model => model.Phone) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Active) %>
</div>
<div class="editor-field">
<%: Html.CheckBoxFor(model => model.Active) %>
<%: Html.ValidationMessageFor(model => model.Active) %>
</div>
<p>
<input type="submit" value="<%: Resources.ToSearch %>" id="btnSearch" />
</p>
</fieldset>
<% } %>
<%= Html.Telerik().Grid<SerializableMember>()
.Name("Grid")
.Columns(colums =>
{
colums.Bound(c => c.Email).Title(Resources.Email).ClientTemplate("<a href=\"" + Url.Action(MVC.Member.Edit()) + "/<#=Id#>\" ><#=Email#></a>");
colums.Bound(c => c.FirstName).Title(Resources.FirstName);
colums.Bound(c => c.LastName).Title(Resources.LastName);
colums.Bound(c => c.MemberNumber).Title(Resources.MemberNumber);
colums.Bound(c => c.Phone).Title(Resources.Phone);
colums.Bound(c => c.Active).Title(Resources.Active).HeaderHtmlAttributes(new { #class = "center-text" }).HtmlAttributes(new { #class = "center-text" }).ClientTemplate("<img src=\"Content/images/icons/<#=Active#>.png\" alt=\"<#=Active#>\" />");
colums.Bound(c => c.Id).Title(" ").HtmlAttributes(new { #class = "center-text" }).ClientTemplate("<a href=\"" + Url.Action(MVC.Member.ResetPassword()) + "/<#=Id#>\" title=\"" + Resources.ResetPassword + "\" >" + Resources.ResetPassword + "</a>");
colums.Bound(c => c.Id).Title(" ").HtmlAttributes(new { #class = "center-text" }).ClientTemplate("<a href=\"" + Url.Action(MVC.Member.Activate()) + "/<#=Id#>\" title=\"" + Resources.Activate + "\" >" + Resources.Activate + "</a>");
colums.Bound(c => c.Id).Title(" ").HtmlAttributes(new { #class = "center-text" }).ClientTemplate("<a href=\"" + Url.Action(MVC.Member.Deactivate()) + "/<#=Id#>\" title=\"" + Resources.Deactivate + "\" >" + Resources.Deactivate + "</a>");
})
.DataBinding(d => d.Ajax().Select(MVC.Member.ListAjax(Model).GetRouteValueDictionary()))
.Sortable()
.NoRecordsTemplate(Resources.NoData)
%>
<%= Html.AntiForgeryToken() %>
<script type="text/javascript">
$(document).ready(function () {
$('#btnSearch').click(function () {
var grid = $('#Grid').data('tGrid');
var searchModel = {
MemberNumber: $('#MemberNumber').val(),
Email: $('#Email').val(),
FirstName: $('#FirstName').val(),
LastName: $('#LastName').val(),
Phone: $('#Phone').val(),
Active: $('#Active').is(':checked')
};
grid.rebind(searchModel);
return false;
});
});
</script>
<%= Html.Telerik().ScriptRegistrar().jQuery(false).DefaultGroup(g => g.DefaultPath("~/Content/Javascript/2010.3.1110"))%>
Yeah, you need to url encode it: name%2B1%40domain.com. + in a url means white space. Notice also that you need to url encode # to %40 because it also has special meaning.
Conclusion: always url encode your urls. Always use Url helpers to generate urls. Never hardcode urls.
UPDATE:
Try:
var searchModel = {
MemberNumber: encodeURIComponent($('#MemberNumber').val()),
Email: encodeURIComponent($('#Email').val()),
FirstName: encodeURIComponent($('#FirstName').val()),
LastName: encodeURIComponent($('#LastName').val()),
Phone: encodeURIComponent($('#Phone').val()),
Active: $('#Active').is(':checked')
};
I think this might be the browser eating it up. Check the actual output of your code instead of looking at your browser status bar.
If you want an actual +, you might need to URL encode it (replace it with %2B, although you might want to consider other characters as well).
The title just about says it all. I have a complex EF4 entity object that has a list of smaller objects I'd like to bind to checkboxes within my view. I just can't figure out how to satisfy the first argument of Html.CheckboxFor(). Intellisense keeps giving me an error.
Here's my view models:
public class PlatformListing
{
public Platform Platform { get; set; }
public bool IsSelected { get; set; }
}
public class AdminGameReviewViewModel
{
public Game GameData { get; set; }
public List<Genre> AllGenres { get; set; }
public List<PlatformListing> AllPlatforms { get; set; }
}
And my (most likely horrible) controller code which populates the AdminGameReviewViewModel and sends it to the view:
public ActionResult EditReview(int id)
{
var game = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").Single(g => g.GameID == id);
var genres = _siteDB.Genres.OrderBy(g => g.Name).ToList();
var platforms = _siteDB.Platforms.OrderBy(p => p.Name).ToList();
List<PlatformListing> platformListings = new List<PlatformListing>();
foreach (Platform platform in platforms)
{
platformListings.Add(new PlatformListing { Platform = platform, IsSelected = game.Platforms.Any(p => p.PlatformID == platform.PlatformID) ? true : false });
}
var model = new AdminGameReviewViewModel { GameData = game, AllGenres = genres, AllPlatforms = platforms };
return View(model);
}
I'm just not sure what I'm missing, and it's driving me nuts. The documentation I've found hasn't really shed any light on it, either.
EDIT: relevant view code (partial view to be used for both Create and Edit) -
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HandiGamer.ViewModels.AdminGameReviewViewModel>" %>
<p>
<%: Html.Label("Game Title") %>
<%: Html.TextBoxFor(model => model.GameData.GameTitle) %>
<%: Html.ValidationMessageFor(model => model.GameData.GameTitle) %>
</p>
<p>
<%: Html.LabelFor(model => model.GameData.Genre) %>
<%: Html.DropDownList("Genre", new SelectList(Model.AllGenres, "GenreID", "Name", Model.GameData.GenreID)) %>
</p>
<p>
<%: Html.Label("Platforms") %><br />
<% foreach(var item in Model.AllPlatforms) { %>
<%: item.Platform.Name %> <%: Html.CheckBox("Platforms[]", item.IsSelected, new { id = item.Platform.PlatformID }) %><br />
<% } %>
</p>
<p>
<%: Html.Label("Review Title") %>
<%: Html.TextBoxFor(model => model.GameData.Content.Title) %>
</p>
<p>
<%: Html.Label("Review") %>
<%: Html.TextAreaFor(model => model.GameData.Content.Text) %>
</p>
<p>
<%: Html.Label("Review Score") %>
<%: Html.DropDownList("Score", new SelectList(new int[] {1, 2, 3, 4, 5}, "ReviewScore")) %>
</p>
<p>
<%: Html.LabelFor(model => model.GameData.Pros) %><br />
<%: Html.TextBox("Pros[]") %><br />
<%: Html.TextBox("Pros[]") %><br />
<%: Html.TextBox("Pros[]") %><br />
<%: Html.TextBox("Pros[]") %><br />
<%: Html.TextBox("Pros[]") %>
</p>
I believe you want something like:
Html.CheckBoxFor(model => model.AdminGameReviewViewModel[i].IsSelected)
within some loop that's defined i in your View. Posting your View might help make this clearer.
I have two an action method
- > RoleURLManagement
which differs with its input parameter in the get compared to the post so we have
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult RoleURLManagement(string id)
{
}
and
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RoleURLManagement(aspnet_Roles rt)
{
}
The get is bringing back a page based on the id in the URL.
The Post should be updating that record.
Now,
this works perfect locally on my machine,
But I have deployed it, It does not recognise the Post at all.
Not sure why this is happening, on the view I have a BeginForm posting to this method.
Wow
HTML
<% using (Html.BeginForm("RoleURLManagement", "Role", FormMethod.Post))
{%>
<fieldset>
<%-- <legend>Fields</legend>--%>
<div class="display-label">ApplicationId</div>
<div class="display-field"><%: Model.ApplicationId%></div>
<%: Html.TextBoxFor(model => model.ApplicationId, new {#class = "RemoveAttribute" })%>
<div class="display-label">RoleId</div>
<div class="display-field"><%: Model.RoleId%></div>
<%: Html.TextBoxFor(model => model.RoleId, new {#class = "RemoveAttribute" })%>
<div class="display-label">RoleName</div>
<h1><div class="display-field"><%: Model.RoleName%></div></h1>
<%: Html.TextBoxFor(model => model.RoleName, new {#class = "RemoveAttribute" })%>
<%: Html.TextBox("RoleName") %>
<div class="display-label">LoweredRoleName</div>
<div class="display-field"><%: Model.LoweredRoleName%></div>
<%: Html.TextBoxFor(model => model.LoweredRoleName, new {#class = "RemoveAttribute" })%>
<div class="display-label">Description</div>
<div class="display-field"><%: Model.Description%></div>
<%: Html.TextBoxFor(model => model.Description, new {#class = "RemoveAttribute" })%>
</fieldset>
<div class="siteCheck">
<%=Html.SiteMapCheckBoxManagement("checkManagememt", Model)%>
<%=Html.TextArea("t")%>
</div>
<input type="submit" value="Map Sites to Role" />
<% } %>
Hmm... not sure why that isn't work... here are a few stabs at ideas:
1) You aren't passing the id into the post method?
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult RoleURLManagement(string id)
{
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RoleURLManagement(string id, aspnet_Roles rt)
{
}
2) See if it works when accepting a FormCollection instead of aspnet_Roles: (then get the role based on ID, and do an UpdateModel(role) to apply the changes)
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult RoleURLManagement(string id)
{
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RoleURLManagement(string id, FormCollection form)
{
}
Of course neither of those explain why it works different on your machine than the server. Do you have different versions of IIS? RC of MVC 2?
Good Luck...
The contactAddModel.Search always comes through as null - any ideas?
View declaration
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<rs30UserWeb.Models.StatusIndexModel>" %>
ViewModels
public class StatusIndexModel
{
public ContactAddModel contactAddModel;
public StatusMessageModel statusMessageModel;
}
public class ContactAddModel
{
[Required(ErrorMessage="Contact search string")]
[DisplayName("Contact Search")]
public string Search { get; set; }
}
View content
<% using (Html.BeginForm("AddContact", "Status")) { %>
<div>
<fieldset>
<legend>Add a new Contact</legend>
<div class="editor-label">
<%= Html.LabelFor(m => m.contactAddModel.Search) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(m => m.contactAddModel.Search)%>
<%= Html.ValidationMessageFor(m => m.contactAddModel.Search)%>
</div>
<p>
<input type="submit" value="Add Contact" />
</p>
</fieldset>
</div>
<% } %>
Controller
[HttpPost]
public ActionResult AddContact(Models.ContactAddModel model)
{
if (u != null)
{
}
else
{
ModelState.AddModelError("contactAddModel.Search", "User not found");
}
return View("Index");
}
You should modify the action AddContact like this
AddContact(Models.ContactAddModel contactAddModel)
just replace "model" with "contactAddModel"