connecting an action of button in view in asp.net mvc3 - asp.net-mvc

I am beginner in asp.net mvc . I am going to connect an action of button in view.
but i cant. i work with web form, in fact i want to click on the button, create action will be called and insert the data.
my code is as following:
The Controller:
namespace BookStore.Controllers
{
public class BookController : Controller
{
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(book bookobj)
{
var dBook=new DBook();
dBook.Insertbook(bookobj);
return RedirectToAction("Index", "Home");
}
}
}
The View:
#model BookStore.Models.DomainObject.book
#{
ViewBag.Title = "Create";
}
<h2>insert data/h2>
#using(Html.BeginForm())
{
#Html.ValidationSummary(true);
<fieldset>
<div>
#Html.LabelFor(model=> model.book_name)
</div>
<div>
#Html.EditorFor(model => model.book_name)
</div>
<div>
#Html.LabelFor(model=>model.book_qty)
</div>
<div>
#Html.EditorFor(model=>model.book_qty)
</div>
<br/>
<div>
<input id="Button_craete_book" type="button" value="insert" />
</div>
</fieldset>
}

Change the button type to "submit":
<input id="Button_craete_book" type="submit" value="insert" />
That will post the form and values to the Edit method in the controller, as marked with the HttpPost attribute.

Related

Pass argument to controller on submit

So, i started learning MVC, and i need to pass an email to a controller. (Trying to make a standard email signup)
Therefore i have an input and a button which (should) pass the input to an argument accepting controller and then redirect to another view.
I have the following controllers:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string mail)
{
person = new EmailSignup{Email = mail};
return RedirectToAction("details");
}
public ActionResult details()
{
return View(person);
}
This is what i have in my View:
#using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<form class="col-md-12">
<div class="form-group form-inline">
<label class="margin20">Sign up for newsletter</label>
<input type="Email" class="form-control" style="display:inline-block; max-width:200px" id="mail" placeholder="Example#Example.com" />
<button type="submit" class="btn btn-default" style="display:inline-block" id="emailSignup">Signup</button>
</div>
</form>
}
It redirects to my "details" view, but my email is not showing.
Furthermore, is this best practice? would i want to do it like this?
#using (Html.BeginForm("Index", "Home", FormMethod.Post)) renders a form, you don't need a second one inside it (if you need to add the class, you can use an overload of Html.BeginForm). Your input contains an id property, but not a name property. name is what's used when an action happens inside a form.

How can i invoke a method in a different controller including parameters using mvc 6

I'm new to developing web applications and most importantly new to mvc. I'm trying to navigate from one view to another controller action including parameters. I have the below code in my currently displaying view:
<p>
<a asp-controller="Working_set" asp-action="Create">Create new Working set</a>
</p>
#foreach (var item in Model)
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail tile tile-medium">
<a asp-controller="SelectedWorking_set" asp-action="index">
<h2>
#Html.DisplayFor(modelItem => item.Name)
<input name="workingSetID" type="hidden" value="#item.Working_setID" />
</h2>
</a>
</div>
</div>
}
How can i use the Working_setID in the controller SelectedWorking_set below:
[Route("SelectedWorking_set")]
public class SelectedWorking_setController: Controller
{
private FlightmapContext _context;
public SelectedWorking_setController(FlightmapContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
[HttpPost("Index")]
public IActionResult Index([FromBody]int workingSetID)
{
//return View(_context.Project.ToList());
return View();
}
}
You need to pass input name to your controller method like below example;
<input name="workingSetID" type="hidden" value="#item.Working_setID" />
here "workingSetID" name we need to pass your controller method, Suppose your method name is "Index" then you need to write like this in your controller.
[HttpPost]
public ActionResult Index(string workingSetID)
{
//Code here
}
like this you will get "Working_setID" value in your controller method. And also you need submit button to post this value to controller.

ASP.net MVC - how does controller action method gets parameter value?

I know the title of this question isn't very pretty. I dint have a better way to put it.
Lets look at the following code :
View for delete action:
#model LearningMVC.Models.Blog
#{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Blog</legend>
<div class="display-label">
#Html.DisplayNameFor(model => model.URL)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.URL)
</div>
</fieldset>
#using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
#Html.ActionLink("Back to List", "Index")
</p>
}
Controller action for delete action
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Blog blog = db.Blogs.Find(id);
db.Blogs.Remove(blog);
db.SaveChanges();
return RedirectToAction("Index");
}
My questions is, How on earth this method got its parameter ?!

Asp.net mvc delete with AcceptVerbs HttpVerbs.Post method auto invoke problem

I'm new to Asp.net MVC.
I have an application that manages users. My problem is that when I click on a delete link of a user row, the delete method has [AcceptVerbs(HttpVerbs.Post)] annotation is called automatically after a render delete method invoked. Thus, the user with id is deleted before I click on the delete confirm button and the error has occurred afterward.
Everything worked fine before. So, I don't know why this happend.
My code:
// GET: /User/Delete/5
[Authorize]
public ActionResult Delete(int id)
{
return View(GetByUserId(id));
}
// POST: /User/Delete/5
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(Users user)
{
//some process
}
The second method invokes automatically after the first one is called.
The view:
<h2>Delete</h2>
<p class="error"><%= Html.Encode(ViewData["messages"]) %></p>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Fields</legend>
<div class="display-label">UserId</div>
<div class="display-field"><%= Html.Encode(Model.UserId) %></div>
<div class="display-label">UserName</div>
<div class="display-field"><%= Html.Encode(Model.UserName) %></div>
<div class="display-label">FullName</div>
<div class="display-field"><%= Html.Encode(Model.FullName) %></div>
<div class="display-label">Email</div>
<div class="display-field"><%= Html.Encode(Model.Email) %></div>
<div class="display-label">DayOfBirth</div>
<div class="display-field"><%= Html.Encode(String.Format("{0:MM/dd/yyyy}", Model.DayOfBirth))%></div>
<div class="display-label">Phone</div>
<div class="display-field"><%= Html.Encode(Model.Phone) %></div>
<div class="display-label">Active</div>
<div class="display-field"><%= Html.Encode(Model.Active) %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" /> |
<%= Html.ActionLink("Back to List", "Index") %>
</p>
<% } %>
You can create another view which will solve your purpose of confirmation delete.
Please find more details using following link.
http://forums.asp.net/t/1513258.aspx
Please find your modify code below.
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Delete()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(RegisterModel ob)
{
return RedirectToAction("Index");
}
}
Index.cshtml
#{
ViewBag.Title = "Home Page";
}
<h2>#ViewBag.Message</h2>
<h1>
#Convert.ToString(ViewBag.Delete)
</h1>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
<br />
<br />
#Html.ActionLink("Delete record", "Delete");
<br />
<br />
</p>
delete.cshtml
#model MvcApplication1.Models.RegisterModel
#{
ViewBag.Title = "Delete";
}
<h2>
Delete</h2>
<div>
Are you sure you want to delete this?</div>
#using (Html.BeginForm())
{
<p>
<input type="submit" value="Delete" />
|
</p>
}
Above code is same as your code and it's working fine. Please have a look.

keeping Object Data after passing it to a strongly typed view

I have a simple ASP.NET MVC 3 site, from my Controller out of the Edit Action I pass a Object (a class which is also mapped by nhibernate)
After editing and clicking save i pass it to the [HTTPPost] decoraded Method but and all properties are correct, excerpt the "id" property it hat a guid value equivalent to NULL (00000000-0000-000...).
Is there a problem using the Domain Model to strongly type my Views? May the problem be that Id has:
{get; private set;}
???
Thanks in advance.
Here The Code:
My View:
'#model fnh.DataModel.Kunde
#{
View.Title = "EditKunde";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>EditKunde</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Fields</legend>
#Html.EditorFor(model => model._id)
#Html.EditorFor(model => model._KdNr);
<div class="editor-label">
#Html.LabelFor(model => model._Name)
`enter code here` </div>
<div class="editor-field">
#Html.EditorFor(model => model._Name)
#Html.ValidationMessageFor(model => model._Name)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
'
My Controller Actions:
' public ActionResult EditKunde(Guid id)
{
return View(_kunden.GetKundeById(id));
}
[HttpPost]
public ActionResult EditKunde(Kunde kunde)
{
Ansprechpartner anp = new Ansprechpartner();
anp._Name = "JustATry";
kunde._Ansprechpartner.Add(anp);
`enter code here` _kunden.EditKunde(kunde);
return View();
}'
Are you adding the Id property to your form (maybe as a hidden field)?
It would be useful if you post the Action code (for both Edit Actions), and the View.

Resources