I am trying to add a new User in my database, but every time I press submit i get this error
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. "
This is my controller:
public class CreateController : Controller
{
// GET: Create
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(User user)
{
using (var db = new UserEntities())
{
User newUser = new User();
newUser.Name = user.Name;
db.Users.Add(newUser);
db.SaveChanges();
return View();
}
}
}
This is my view:
#model FormulörModul.Models.User
<title>Create</title>
#using (Html.BeginForm("Create", "CreateController", FormMethod.Post))
{
<label>Namn</label>
#Html.TextBoxFor(m=>m.Name)
#Html.ValidationMessageFor(m => m.Name)
<input id="Submit1" type="submit" value="submit" />
}
I think the error is in the call to Html.BeginForm. By convention in MVC, when you name a controller you just name the string before "Controller". I.e. replace this
#using (Html.BeginForm("Create", "CreateController", FormMethod.Post))
with this
#using (Html.BeginForm("Create", "Create", FormMethod.Post))
BTW, "Create" is a really bad name for a controller. I would name it "User".
Related
Not sure why this isn't working, but I suspect it's something to do with routing... (Using MVC5)
When clicking on the submit button I get the following message:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /contact
Using a model as follows:
public class EmailMessageModel
{
/// <summary>Gets or sets from name.</summary>
/// <value>From name.</value>
[Required, Display(Name = "Name")]
public string FromName { get; set; }
}
The view is then as follows:
#model EmailMessageModel
#using (Html.BeginForm("index", "contact", FormMethod.Post, new { enctype = "multipart/form-data", #class = "contact-form" }))
{
#Html.AntiForgeryToken()
#Html.LabelFor(m => m.FromName, new { #class = "control-label" })
#Html.TextBoxFor(m => m.FromName, new { #class = "form-control" })
#Html.ValidationMessageFor(m => m.FromName)
<input type="submit" value="Send Message" id="btnSubmitQuery" />
}
Controller is as follows:
(the breakpoint on the HttpPost Index action is never hit, any ideas why?)
namespace ExternalSite.Controllers
{
using ExternalSite.Models;
using System.Net.Mail;
using System.Web.Mvc;
[RoutePrefix("contact")]
public class ContactController : Controller
{
[HttpGet]
[Route]
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(EmailMessageModel model)
{
// !!!!!!!!!!BREAKPOINT HERE IS NEVER BEING HIT!!!!!!!!!!!
if (ModelState.IsValid)
{
}
return View(model);
}
}
The solution was to add the blank route attribute [Route] to the HttpPost Index method, i.e.
[HttpPost]
[Route]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(EmailMessageModel model)
{
// !!!!!!!!!!BREAKPOINT HERE IS NEVER BEING HIT!!!!!!!!!!!
if (ModelState.IsValid)
{
}
return View(model);
}
suppose i have a form like this
#using (Html.BeginForm("submit", "home", FormMethod.Post))
{
#Html.TextBoxFor(x => x.firstname, new { placeholder = "firstname" })
#Html.TextBoxFor(x => x.lastname, new { placeholder = "lastname" })
<input type="submit" value="submit" />
}
and i have the method as follows
public ActionResult Index()
{
return View(new student());
}
[HttpPost]
public ActionResult submit(student _student) {
return View("index", new student());
}
my question is when i make a post request to the same method ie. index by passing an empty object, the controls are getting cleared but when i make a post request to the submit method the value of the controls is not getting cleared. why is that happening? why do i have to clear it using
ModelState.Clear() but not by passing any empty object?
I am new to asp.net MVC5 and i am trying to pass data from view to controller as a string.
Here is controller class:
namespace Movies.Controllers
{
public class HelloWorldController : Controller
{
// GET: HelloWorld
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult welcome(FormCollection fc, string reportName)
{
ViewBag.Message = reportName;
return View();
}
}
}
Here is Index View:
#{
ViewBag.Title = "MVC Movies";
}
<h2>My Movies List</h2>
<p>Hellow from our view template</p>
#using (Html.BeginForm("Welcome", "HelloWorld", FormMethod.Get))
{
<p>
<input type="text" name="reportName" />
<input type="submit" />
</p>
}
Here is welcome view:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>welcome</h2>
#{
ViewBag.Title = "Welcome";
}
<ul>
#for (int i = 0; i < 2; i++)
{
<li>#ViewBag.Message</li>
}
</ul>
Actually my Index method is passing a view that will have a textbox for string in it in a form and then on clicking submit button application should pass that string in the Welcome method in the same controller. On clicking submit button browser is showing a windows that resources con't be found.
Whats the problem..? thank you for your time..:)
1) The action name is case sensitive, you are using "Welcome" in the form definition , but the action must has the name "welcome" with w lower case.
2) Your form is doing a GET but you are specting a POST in the action
[HttpGet]
public ActionResult welcome(FormCollection fc, string reportName)
{
ViewBag.Message = reportName;
return View();
}
Replace FormMethod.Get with FormMethod.Post in your beginform.
Modify FormMethod.Get to FormMethod.Post:
#using (Html.BeginForm("Welcome", "HelloWorld", FormMethod.Post))
{
<p>
<input type="text" name="reportName" />
<input type="submit" />
</p>
}
Change your action implementation to this:
[HttpPost]
public ActionResult welcome(FormCollection fc)
{
ViewBag.Message = fc["reportName"];
return View();
}
However, I'd strongly suggest that you create a view model for your form and use it instead of FormCollection.
I've hijacked the route in Umbraco 7.1 and for some reason my HttpPost is not firing when the submit button is pressed. Any input as to why this is? There is a postback taking place when send is pressed but the when putting a break point in the HttpPost it's never fired.
Here's a snippet of my code, the markup followed by the controller.
#inherits UmbracoViewPage
#{
Layout = "Layout.cshtml";
}
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.TextAreaFor(m => m.Message)
< i n p u t type="submit" value="Send" />
#Html.ValidationMessageFor(m => m.Message)
</div>
}
public ActionResult Index(ManageMessageId? smess)
{
var errorModel = new ErrorModel();
...
return CurrentTemplate(errorModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(ErrorModel model)
{
if (ModelState.IsValid)
{
...
}
return View();
}
Assuming you are using SurfaceControllers, you would want to create your form as follows. Note the change in how you create the form and how the generic and parameter match that of the surface controller:
#using (Html.BeginUmbracoForm<MyController>("Index"))
{
}
Your controller should look something like:
public class MyController : SurfaceController
{
public ActionResult Index(ManageMessageId? smess)
{
var errorModel = new ErrorModel();
...
return CurrentTemplate(errorModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(ErrorModel model)
{
if (ModelState.IsValid)
{
...
}
return View();
}
}
I have MVC3 razor application. Once I'm submitting a form and in Action i'm changing ViewModel content, i can't see new values populated.
There was a topic about that in MVC2 where guys told that it may be fixed in MVC3
http://aspnet.codeplex.com/workitem/5089?ProjectName=aspnet
Can you tell if there is an option to do that or what is the better way(workaround) to update UI without JavaScript using postbacks?
Action:
[HttpPost]
public ActionResult Index(MyViewModel model)
{
model.Value = "new value"
return View("Index", model);
}
UI:
#Html.HiddenFor(x => x.Value)
ViewModel:
public class MyViewModel
{
public string Value { get;set; }
}
Looks like it's using the ModelState values that were posted.
If you clear the ModelState using ModelState.Clear() the new value you set should be in the hidden field.
You should use form and to post it to action.
#model MyViewModel
#using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
#Html.HiddenFor(x=>x.Value)
<input type="submit" value="Submit" />
}
Controller
//
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.Value = "old value";
return View("Index", model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
//get posted model values (changed value by view "new value")
string changed_value = model.Value;
// you can return model again if your model.State is false or after update
return View("Index", model);
}