Firefox Request.IsAjaxRequest() doesn't work second time - asp.net-mvc

I have a problem with Request.IsAjaxRequest() method and Firefox seems to be the only browser where it doesn't work properly.
I have a PartialView and I use Ajax.BeginForm() for reloading only that part of the page:
#using (Ajax.BeginForm(new AjaxOptions() { UpdateTargetId = "form0", OnSuccess = "$.validator.unobtrusive.parse('form0');" }))
{
<div>
<fieldset>
<h3>
Login
</h3>
<div class="required">
#Html.LabelFor(m => m.Username)
#Html.TextBoxFor(m => m.Username)
#Html.ValidationMessageFor(m => m.Username)
</div>
<div class="required">
#Html.LabelFor(m => m.Password)
#Html.PasswordFor(m => m.Password)
<input type="submit" value="Login" />
#Html.ValidationMessageFor(m => m.Password)
</div>
<div class="checkboxContainer">
#Html.ActionLink("Forgot?", "ForgotPassword")
#Html.CheckBoxFor(m => m.RememberMe)
#Html.LabelFor(m => m.RememberMe)
</div>
</fieldset>
</div>
}
In controller I have code:
if (Request.IsAjaxRequest())
{
return PartialView("~/Views/Account/_Login.cshtml", model);
}
else
{
return View(model);
}
because it needs to work without javascript enabled also.
Thing is that Firefox only first time return PartialView and second time return View. Other browsers (Opera, IE, Chrome) works fine and always return PartialView.

Related

Ajax form with model errors

I created an Ajax form in mvc 4 like that
#using (Ajax.BeginForm("Create", ajaxOptions: new AjaxOptions { UpdateTargetId = "updatedDiv", OnFailure = "OnFailure" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary()
<div id="Errors" class="block">
</div>
#*<div class="block width-50">
<div class="input-group" style="direction: ltr">
<input type="text" class="form-control">
<span class="input-group-addon">Incoming No</span>
</div>
</div>*#
<div class="block" id="item-guarantee">
#Html.RadioButtonFor(m => m.CorrespondenceSideId, 1, new { #checked = "checked" })<label class="label-style">#Global.CorrespondenceSide_Internal</label>
#Html.RadioButtonFor(m => m.CorrespondenceSideId, 2)<label class="label-style">#Global.CorrespondenceSide_External</label>
#Html.ValidationMessageFor(m => m.CorrespondenceSideId)
</div>
<div class="block">
<div class="input-group">
#Html.Label(Global.Correspondence_CorrespondenceNo)
#Html.EditorFor(m => m.CorrespondenceNo)
#Html.ValidationMessageFor(m => m.CorrespondenceNo)
</div>
</div>
<div class="block">
<div class="input-group">
#Html.Label(Global.Correspondence_IncomingNo)
#Html.EditorFor(m => m.IncomingNo)
#Html.ValidationMessageFor(m => m.IncomingNo)
</div>
</div>
}
and this is the controller
public ActionResult Create(Correspondence model)
{
ViewBag.BoxType = (Enum_BoxType)model.BoxTypeId;
ViewBag.CorrespondenceTypesList = this.Factory.Get<CorrespondenceTypesService>()
.GetAsListItems(this.CurrentLanguage);
ViewBag.AssignmentSidesList = this.Factory.Get<AssignmentSidesService>()
.GetAsListItems(this.CurrentLanguage);
ViewBag.IncomingSidesList = this.Factory.Get<IncomingSidesService>()
.GetAsListItems(this.CurrentLanguage);
ViewBag.Lookup_BoxTypes = this.Factory.Get<BoxTypesServices>().GetAsListItems(this.CurrentLanguage, (int)ViewBag.BoxType);
if (!ModelState.IsValid) return PartialView("_CreatePartial", model);
this.Factory.Get<CorrespondencesService>().AuthorizedAdd(model);
var AllCorrespondences = this.Factory.Get<CorrespondencesService>().GetAll(correspondence => correspondence.Date, (Enum_BoxType)model.BoxTypeId).ToList();
return new View("Index", AllCorrespondences.ToPagedList(1, 5));
}
Now when I return PartialView("_CreatePartial", model); and model returns with model errors, that errors doesn't appear in 'validationsummary()' in the partial

asp.net mvc 4 - form submit button not responding

I am new to MVC and is using MVc4 with VS2013. This is my controller:
[HttpPost]
public ActionResult Create(CreateRequestViewModel viewModel)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
return View(viewModel);
}
Below is my view:
#model ProMs.Web.ViewModels.CreateRequestViewModel
#{
ViewBag.Title = "Create";
}
<body>
<h2>New Request</h2>
<h3></h3>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<div class="float-left">
<label for="RequestName">Request</label>
#Html.EditorFor(model => model.Request.RequestName)
#Html.ValidationMessageFor(model => model.Request.RequestName)
#Html.LabelFor(model => model.Request.Requestor)
#Html.EditorFor(model => model.Request.Requestor)
#Html.ValidationMessageFor(model => model.Request.Requestor)
#Html.LabelFor(model => model.Request.Purpose)
#Html.EditorFor(model => model.Request.Purpose)
#Html.ValidationMessageFor(model => model.Request.Purpose)
</div>
<div class="float-right">
#Html.LabelFor(model => model.Request.Investigator)
#Html.EditorFor(model => model.Request.Investigator)
#Html.ValidationMessageFor(model => model.Request.Investigator)
#Html.LabelFor(model => model.Request.Department)
#Html.EditorFor(model => model.Request.Department)
#Html.ValidationMessageFor(model => model.Request.Stage)
#Html.LabelFor(model => model.Request.Comment)
#Html.EditorFor(model => model.Request.Comment)
#Html.ValidationMessageFor(model => model.Request.Comment)
</div>
#Html.HiddenFor(model => model.Request.RequestID)
#Html.HiddenFor(model => model.Request.DateCreated)
#Html.HiddenFor(model => model.Request.CreatedBy)
</fieldset>
}
<p>
<input type="submit" value="Submit" />
</p>
</body>
Nothing happened if "submit" button is clicked. I even cold not put a breaking point at the line
.
Thanks very much for your help.
Hugh
#Html.BeginForm() creates a <form> element. Right now your submit button is outside of this element, so move it inside.
}
<p>
<input type="submit" value="Submit" />
</p>
Should be
<p>
<input type="submit" value="Submit" />
</p>
} <-- This goes down here

Html.DisplayFor not posting values to controller in ASP.NET MVC 3

I am using ASP.NET MVC 3 with Razor, below is a sample from my view code.
The user should be able to edit all their details, except the "EmailAddress" field. For that field only I have used Html.DisplayFor(m => m.EmailAddress).
But when this form gets posted, all the model properties are filled except the EmailAddress.
How do I get the email back in the model when posting? Should I have used some helper other than DisplayFor?
#using (Html.BeginForm()) {
#Html.ValidationSummary(true, "Account update was unsuccessful. Please correct any errors and try again.")
<div>
<fieldset>
<legend>Update Account Information</legend>
<div class="editor-label">
#Html.LabelFor(m => m.EmailAddress)
</div>
<div class="editor-field">
#Html.DisplayFor(m => m.EmailAddress)
#*#Html.ValidationMessageFor(m => m.EmailAddress)*#
</div>
<div class="editor-label">
#Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.FirstName)
#Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.LastName)
#Html.ValidationMessageFor(m => m.LastName)
</div>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
</div>
}
Please advise me on this.
you'll need to add a
#Html.HiddenFor(m => m.EmailAddress)
DisplayFor won't send anything in POST, it won't create an input...
By the way, an
#Html.HiddenFor(m => m.Id) // or anything which is the model key
would be usefull

JQuery UI Tab with form in ASP.NET MVC 3

I try to make user registation form. I have two user types and want to make two JQuery UI tabs with forms. But tab is empty and in java script console error "GET http://localhost/ParcDocs/Admin/Users/AddWorker 500 (Internal Server Error)".
Code of user registration page:
<script type="text/javascript">
$(document).ready(function() {
$("#tabContainer").tabs();
});
</script>
<div id="tabContainer">
<ul>
<li>#Html.ActionLink("Пользователь", "AddUser", "Users", null, null)</li>
<li>#Html.ActionLink("Сотрудник", "AddWorker", "Users", null, null)</li>
</ul>
</div>
PartialView code:
#model ParcDocs.Models.WorkerUser
#using (Html.BeginForm("AddWorker", "Users"))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Пользователь</legend>
<div class="editor-label">
#Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserName)
#Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserName)
#Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.RoleId, ((IEnumerable<ParcDocs.Models.Role>)ViewBag.PossibleRoles).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Name),
Value = option.Id.ToString(),
Selected = (Model != null) && (option.Id == Model.RoleId)
}), "Выберете роль пользователя")
</div>
<p>
<input type="submit" value="Добавить" />
</p>
</fieldset>
}
Controller code:
public ActionResult AddWorker()
{
var model = new WorkerUser();
return PartialView(model);
}
Same behavior this second tab.
You should be returning a PartialViewResult, rather than an ActionResult. Put a breakpoint on public ActionResult AddWorker() and see what exception you get. Paste it here so that we have more information.

MVC 3 - Posting partial view to a chosen controller

I'm trying to learn MVC 3 and Razor coming from a ASP.NET background.
I've want to get a simple partial view (in the shared folder) to post to a specific controller so that I can re-use it elsewhere such as for articles, blogs etc. I tried using variations of the following.
#using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { }))
{
<div>
<fieldset>
<legend>Comments</legend>
<div >
#Html.LabelFor(m => m.Name)
#Html.TextBoxFor(m => m.Name)
</div>
<div >
#Html.LabelFor(m => m.Email)
#Html.TextBoxFor(m => m.Email)
</div>
<div >
#Html.LabelFor(m => m.Body)
#Html.TextBoxFor(m => m.Body)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</div>
}
This doesn't post to the comments controller action Create as shown below.
[HttpPost]
public ActionResult Create()
{
// Save comment code here
return View();
}
There is any simple way of doing this without having to bound to a specfic route?
I found the answer.
#using (Ajax.BeginForm("Create", "Comment", new AjaxOptions() {
UpdateTargetId = "MainContainer" }))
{
<div>
<fieldset>
<legend>Comments</legend>
<div >
#Html.LabelFor(m => m.Name)
#Html.TextBoxFor(m => m.Name)
</div>
<div >
#Html.LabelFor(m => m.Email)
#Html.TextBoxFor(m => m.Email)
</div>
<div >
#Html.LabelFor(m => m.Body)
#Html.TextBoxFor(m => m.Body)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</div>
}
This posts back using ajax and doesn't change the URL. Or you can do it this way using JQuery http://jvance.com/blog/2010/02/20/MakingAnAjaxFormWithJQueryInASPdotNETMVC.xhtml

Resources