I use in view
using (Html.BeginForm("Test", "AcceptStatement", "Account", FormMethod.Post, new { id = Model.StatementID }))
in controller:
public ActionResult AcceptStatement(int id)
but id parameter in controller ends up being a null value. How can I get the id parameter value into my controller with Html.BeginForm?
You're using the wrong overload of BeginForm. You are currently passing the following:
actionName: "Test"
controllerName: "AcceptStatement"
routeValues: "Account"
formMethod: FormMethod.Post
htmlAttributes: { id = Model.StatementID }
This is obviously wrong as it makes no sense. You probably wanted:
Html.BeginForm("AcceptStatement", "Account", new { id = Model.StatementID }, FormMethod.Post, null)
Using this overload.
Use the information in this:
Basically, your view:
#using (Html.BeginForm()){
<p> Title: #Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" /></p>
}
</p>
With a Controller of:
public ActionResult Index(string searchString)
{
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(movies);
}
An alternative to passing the model's value to controller method would be to use:
#Html.ActionLink("Details", "Details", new { id=item.BayID })
With your controller method being something like:
public ActionResult Details(int? id)
{
...
Related
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".
View:
#using (Html.BeginForm())
{
#Html.Label("WebLinkLabel")
#Html.Editor("WebLinkTextbox")
#Html.Label("WebEnabledLabel")
#Html.CheckBox("WebEnabledCheckbox")
<input name="Save" type="submit" value="Save" />
}
Action Method:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
foreach (var key in collection.AllKeys)
{
var typee = collection.GetType();
}
return View();
}
GetType() gives me that:
collection.AllKeys.GetType()
{Name = "String[]" FullName = "System.String[]"} System.Type {System.RuntimeType}
I would like to know like the first controller is of type Textbox, another one is Checkbox etc..
I have a create action and a edit action in a Reviews Controller.
My Create ActionLink is:
#Html.ActionLink("Create New", "Create", new { Id = Model.Id })
My create action is :
[HttpGet]
public ActionResult Create(int Id)
{
return View();
}
My Edit ActionLink is:
#Html.ActionLink("Edit", "Edit", new { id=item.Id }
my edit is:
[HttpGet]
public ActionResult Edit(int id)
{
var model = _db.Reviews.Find(id);
return View(model);
}
In my edit view, I have a Action Link called "Back to List" which is:
#Html.ActionLink("Back to List", "Index", new {id = Model.RestaurantId}, null)
It works and takes me back to where I came from...
In my create view, when I put the same thing, I get error message that is in the heading. that Id does not have a value or is null.. So Model.RestaurantId does not have a value..
If I hard code a value ,it works such as:
#Html.ActionLink("Back to List", "Index", "Reviews", new { id = 1 }, null)
What could I be doing wrong...
I am essentially trying to follow Scott Allens MVC4 tutorial.
I am unable to understand why this is happening. I have a reviews controller. Can some one give me suggestions?
Thanks.
Have a viewmodel for your create view, with a property to store the source /parent id and use that as needed.
public classs CreateReviewVM
{
public int SourceID { set;get;}
}
and in your GET action
[HttpGet]
public ActionResult Create(int Id)
{
var vm=new CreateReviewVM { SourceID=id };
return View(vm);
}
and your create view(create.cshtml) which is strongly typed to your CreateReviewVM
#model CreateReviewVM
#Html.ActionLink("Back to List", "Index", "Reviews",
new { id = Model.SourceID }, null)
Turns out, I can use:
#Html.ActionLink("Back to List", "Index", new { id=Request.Params["restaurantId"] }, null)
and it will populate the value.
#Html.DisplayFor() value not change after send data. I read a article about this issue and say it like this; only send data what such as EditorFor, TextBoxFor, TextAreaFor and change state. Is it true? How can I change this value after the postback?
View
#model HRProj.Model.Person
#using(Html.BeginForm("Skills", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){
#Html.HiddenFor(m => m.SkillDoc.Filename)
<span class="file-upload">
<span>Choose a file</span>
<input type="file" name="file" />
</span>
File name : #Html.DisplayFor(m => m.SkillDoc.Filename)
<button>Upload</button>
}
Controller
public ActionResult Skills(int? id)
{
Others oparations...
var model = new Person { SkillDoc = db.GetSkillDoc().FirstOrDefault(m => m.PersonId == id) };
return View(model);
}
[HttpPost]
public ActionResult Skills(Person model, HttpPostedFileBase file)
{
Others oparations...
if (ModelState.IsValid)
{
SkillDoc doc = new SkillDoc();
doc.Id = model.SkillDoc.Id;
doc.PersonId = model.SkillDoc.PersonId;
doc.CvDoc = (file != null) ? file.FileName : model.SkillDoc.CvDoc;
db.SkillDocCRUD(doc, "I");
TempData["eState"] = "The record adding successfully";
if (file != null)
{
file.SaveAs(Server.MapPath("~/Files/" + file.FileName));
}
}
return View(model);
}
Please add the following line inside the if block:
model.SkillDoc=doc;
Or rather redirect to Skills action:
return RedirectToAction("Skills", new{id= model.PersonId});
I'm trying to open the details of the "Ansatte" object. The problem is that the Int parameter is only null
I have this View
#model IEnumerable<EL4.Administrasjon.Models.Ansatte>
<table>
#foreach (var item in Model) {
<tr data-pkey="#item.IdBaksystem">
<td>
#Html.DisplayFor(modelItem => item.AnsNavn)
</td>
<td>
#Html.DisplayFor(modelItem => item.ePostAdresse)
</td>
<td class="rowControl hidden">
#Html.ActionLink("Details", "Details", new { id=item.ID_Ansatt }) |
</td>
</tr>
}
Then I have this in the controller
public class HMAnsatteController : Controller
{
//
// GET: /HMAnsatte/
public ActionResult Index()
{
HM_000_EL4Entities hmEnt = new HM_000_EL4Entities();
List<Ansatte> HMansatte = hmEnt.Ansatte.ToList();
return View(HMansatte);
}
public ActionResult Details(int? ansattNr)
{
if (ansattNr == null)
{
return null;
}
else
{
Entities ent = new Entities();
Ansatt el4Ansatt = ent.Ansatt.Where(a => a.AnsattNr == ansattNr).First();
return View(el4Ansatt);
}
}
}
When I click details, the URL looks correct: http://localhost:50009/HMansatte/Details/1
But the int ansattNr is always "null"
you have this ...
public ActionResult Details(int? ansattNr)
and it should be this because of the dynamic object you are passing in the link
public ActionResult Details(int? id)
or change this instead ...
#Html.ActionLink("Details", "Details", new { ansattNr=item.ID_Ansatt })
It the line where you are creating your action link, you are naming the parameter "id" but in your action method, you are calling it "ansattNr". Those names should be the same. Since the default routing is already set up for "id", I would just change the method signature of your action method to this:
public ActionResult Details(int? id)
That should solve the problem.
Your passing an variable with a name of id into the controller (which your global.asax has mapped to the URL so it shows like it does) but your controller is looking for a variable with name ansattNr.
You can either change the name of the variable in your controller to id or you can change your Action link to #Html.ActionLink("Details", "Details", new { ansattNr=item.ID_Ansatt }) but the second option will change the formatting on your URL.