MVC 3 - Posting partial view to a chosen controller - asp.net-mvc

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

Related

using viewmodel with create view

I have a ViewModel with two models in it which works find when displaying data. My problem is I want to add a foreach() within the Create.cshtml file. Any ideas?
----Create.cshml-----
#model demo.Models.ViewModel
#{ ViewBag.Title = "Create Reference";}<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Submission Form </legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-field">
<!-- iterate through ExternalContentModel and make checkboxes. -->
#foreach (var item in Model.ExternalContentModel)
{
<label class="checkbox">
<input type="checkbox" name="users" value="#item.auth_lname"> #item.auth_lname
</label>
}
</div>
<div class="editor-label">
#Html.LabelFor(model => model.OrganizationName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OrganizationName)
#Html.ValidationMessageFor(model => model.OrganizationName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.City)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
------Controller-----
//
// GET: /Create
public ActionResult Create() <=== can this be the problem????
{
demo.Models.ViewModel vm = new demo.Models.ViewModel();
vm.ExternalContentModel = _repository.ExternalContent();
// Return the content from the External Model to the Create.
return View(vm);
}
//
// POST: /MailingReferences/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Reference reference) <=== can this be the problem????
{
if (ModelState.IsValid)
{
db.References.Add(reference);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(reference);
}
If you have different models then why don't you just create a editor templates for each model?
You can do this by creating a folder called "EditorTemplates" in the same folder your Create.cshtml view lives in. Now add a view for your model into that folder. The view should be named the same as your model class. E.g. a class called FooBarModel would have an editor template called FooBarModel.cshtml.
You would then just use the editor template by doing #Html.EditorFor(x => x.FooBar)

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

Upload file along with text in Create view

My first .Net/MVC project, I generated a view that allows me to list, edit, and create items in a database, and I would like to add a file upload control to the Create page, to just upload one file.
I understand that within [HttpPost] I need "public ActionResult Index(HttpPostedFileBase file)" but my current [HttpPost] is like this: "public ActionResult Create(lm_pics lm_pics)".
Why do you have 2 file inputs? Why do you have 2 forms? Isn't the first form sufficient (assuming of course you add the enctype="multipart/form-data" attribute to it as you cannot upload files without it)?:
#model PictureUploader_MVC.Models.lm_pics
#{
ViewBag.Title = "Create";
}
<h2>Upload Picture</h2>
#using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>lmit_pics</legend>
<div class="editor-label">
#Html.LabelFor(model => model.brand)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.brand)
#Html.ValidationMessageFor(model => model.brand)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.enabled)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.enabled)
#Html.ValidationMessageFor(model => model.enabled)
</div>
<div>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
</div>
</fieldset>
<button type="submit">Create</button>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
and your controller action that will process the form submission:
[HttpPost]
public ActionResult Create(lm_pics lm_pics, HttpPostedFileBase file)
{
...
}

Firefox Request.IsAjaxRequest() doesn't work second time

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.

Resources