I have the following code in a Partial View that is Rendered inside of another Partial View that is used as the _layout for all views in the project. There is a textarea for comments. I need to get the value of the Textarea to the Action Method in my controller. There is no ViewModel, so I don't know how to capture the contents.
<header>
<h4> Application Notes </h4>
#using (#Html.BeginForm("Comment", "LoanApplication"))
{
#Html.TextArea("Comment")
#*<textarea cols="100" rows="2" name="Comment" placeholder="Leave Comment ..."></textarea>*#
<input value="Add Comment" type="submit" />
}
</header>
The controller code is :
[HttpPost]
[Route("Comment")]
public async Task<ActionResult> Comment(string comment)
{
var loanApplicationServiceProxy = base.ServiceProvider.LoanApplicationServiceProxy;
var applicationComment = new LoanApplicationComment
{
};
await loanApplicationServiceProxy.PutLoanApplicationCommentAsync(applicationComment);
return View();
}
It seems as if it should be something easy, but I can't seem to figure it out. Thanks for any and all assistance.
Change
string comment
To
string Comment
One other thing to try is to receive a FormCollection instead.
Related
I have done some reading and I can't figure out how I change the view, by using the controller to handle the changing of the view. I have 2 buttons that currently use onclick to change the view but I want it done via the controller.
This is my view:
#{
ViewData["Title"] = "RoleCreator";
}
<div class="ui-layout-center">
<h1>Example Header</h1>
<p>Example Text </p>
<input type="button" value="Create Role" onclick="location.href = 'RoleManager/RoleCreator'"/>
<input type="button" value="Edit Role" onclick="location.href='RoleManager/RoleEditor'"/>
</div>
You can use conditions to send different views . give name property to buttons.
On controller side you can call action as per name
[HttpPost]
[ActionName("IndexPost")]
[Button(ButtonName = "Command")]
public ActionResult GetFareFamilies()
{
return View("View1");
}
[HttpPost]
[ActionName("IndexPost")]
[Button(ButtonName = "Save")]
public ActionResult Save(AirPricingWithoutPNRViewModel pricingVm, PricingCommercialDetails commercials)
{
return RedirectToAction("Action name");
}
You can't change the view using the controller. To understand more about the MVC refer to this link and check the relationship between Model-View-Controller.
https://msdn.microsoft.com/en-us/library/dd381412(v=vs.108).aspx
To answer your question you may want to read more about partial views and ajax.
https://www.codeproject.com/articles/698246/asp-net-mvc-special-views-partial-view-and-layout
Using Ajax to render MVC View
Okay I'm new to MVC and trying to make me a webpage where I can transfer to small form box with a zip code and button, to quote page that will fill in the zip code part on the quote page.
My problem is I have two controllers a homeController that has a index view with a small form box. I need to pass the zip code to the QuoteController which has it's own view that populated with the new zip code.
the home controller input, indexview
#using (Html.BeginForm("Quote", "Quote"))
<p>Move From Zip:</p>
<input type="text" name="Zip"/><br />
<input type="submit" value="Next" name="next">
the quote form to receive the zip, on the quote controller, on the quote view
#Html.Label("Move From Zip ")<br />
#Html.TextBoxFor(m => m.MoveFromZip, "", new { maxlength = 5, #class = "short-textbox" })
what's the easiest way of doing this
In your Index view of HomeController, you can keep the form action to "Quote/Quote"
#using (Html.BeginForm("Quote", "Quote"))
{
<input type="text" name="Zip" />
<input type="submit" />
}
Create a view model for your Quote action method view in QuoteController
public class QuoteVm
{
public string Zip { set;get;
}
and in your QuoteController's Quote action method
[HttpPost]
public ActionResult Quote(QuoteVm model)
{
return View(model);
}
and your Quote view will be
#model QuoteVm
<p>Data passed(POSTED) from Index view</p>
#using(Html.BeginForm("QuoteSave","Quote"))
{
#Html.TextBoxFor(s=>s.Zip)
<input type="submit" />
}
Now for your form submit in this view, you need to have another HttpPost action method
[HttpPost]
public ActionResult QuoteSave(QuoteVm model)
{
// to do : Do something and return something
}
I'm trying to call a method from view, but it's not working! :(
My Controller code:
public class ProfessionalController : Controller
{
//
// GET: /Professional/
public ActionResult Index()
{
return View();
}
public ActionResult Sum(int nro1, int nro2)
{
var value = nro1 + nro2;
ViewBag.SumResult = value;
return View();
}
}
My views code:
Professional Index:
#{
ViewBag.Title = "Profissional";
}
<h2>Profissional</h2>
#using (Html.BeginForm("Professional", "Sum")) {
<input id="nro1" type="number" class="form-control marged_top" placeholder="Numero 1" />
<input id="nro2" type="number" class="form-control marged_top" placeholder="Numero 2" />
<button id="Sum" name="Sum" value="Sum" class="btn btn-default" type="button"
data-toggle="button">
Sum
</button>
}
Sum View:
#{
ViewBag.Title = "Sum";
}
<h2>Sum</h2>
#ViewBag.SumResult;
Doesn't occurs any error, just nothing happens when i click at Sum button.
Some issues:
You probably want your <button> with type="submit", otherwise the form will not be submitted, and the button will do nothing. See also: MDN - Button Type
Html.BeginForm("Professional", "Sum") should be Html.BeginForm("Sum", "Professional") - The controller is the second argument.
Html.BeginForm("Sum") would also work here, because this is the current controller.
The inputs should have name="nro1", not id - it is the name that is being submitted.
No need for semicolon after #ViewBag.SumResult (though that isn't a bug).
I'm not sure what data-toggle="button" is doing here.
Note that you are not "calling a Controller from a View" - you are submitting a form, which starts a new Controller-View cycle. Html.Action can do something similar, but that is not what you're trying to do here.
I'd also take this opportunity and recommend against using ViewBag - Use a strongly typed view model instead
I am new to ASP.NET MVC. I was used to program using just ASP.NET. I want to do something when the user clicks a button. I am not understanding what do I do at Controller.
I have this View:
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.Action("RegisterHour", "Register")
and the Controller:
public ActionResult RegisterHour()
{
//TODO: do anything
return View("Index");
}
When I click at the button, I would like to stay in the same page (it can reload). I simply want to do something like go to the database and create a new entity, and then show a messagebox.
This code causes an stackoverflow. What am I missing? What do I have to change at Controller?
Thanks.
The line
#Html.Action("RegisterHour", "Register")
actually makes a request to the server in order to render the result of the "RegisterHour" action. So in order to render the result of the action the first time, you need to make a request to the same action. This causes an endless loop, hence the stack overflow.
You are thinking in events, rather than thinking about HTTP and the web.
ASP.NET MVC embraces the HTTP protocol and you have to know what happens when a request is made and how HTML is rendered.
If you want to implement the scenario you are describing, you have to put a form on the page. The button can submit that form by making a POST request to some other action, and then the action can render a view showing the result. But for simply showing a message box, I don't think it is a good idea.
This is how desktop apps work, not web apps. You are trying to fit a square peg through a round hole.
there are many ways to get back to the controller. without post back look into ajax calls. the simplest way is the post back. put your view in a form tag either html or html.beginform
#using (Html.BeginForm()){
<input type="submit" value="submit"/>
}
as #Chuck mentioned on your controller then have a post method with the same name as the get you show
[HttpPost]
public ActionResult RegisterHour()
{
//TODO: do anything
return View(model);
}
the #Html.Action that you have will return a url so that is put inside another element. something like
<a src="#Html.Action("Action", "Controller")">Click Here</a>
if you want to stay in the same page instead of
return View("index");
use
return View();
Edit:
If you want a complete code of a do something stuff here you are:
Model
public ActionResult MyModel()
{
[Required]
public int propriety1 { get; set; }
}
Controller
public ActionResult DoSomething()
{
var model = new MyModel();
return View(model);
}
[HttpPost]
public ActionResult DoSomething(MyModel model)
{
if(ModelState.isValid){
//DO something
}else{
return View(model);
}
}
View
#model Models.MyModel
#using (Html.BeginForm())
{
#Html.LaberlFor(m=>m.propriety1) #Html.TextBoxFor(m=>m.propriety1)
<input type="submit" value="Click me" />
}
Create an endpoint and have the form submit to it.
UI Code
<form action="/Registration/RegisterHour" >
<p>
<label>Username</label>
<input type="text" name="username" />
</p>
<p>
<label>First Name</label>
<input type="text" name="firstname" />
</p>
<p>
<label>Last Name</label>
<input type="text" name="lastname" />
</p>
<p>
<label>Password</label>
<input type="text" name="password" />
</p>
<p>
<label>Confirm</label>
<input type="text" name="confirm" />
</p>
<input type="submit" value="Save" />
</form>
Model
public class Registration
{
public string Username {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string Password {get; set;}
public string Confirm {get; set;}
}
Typically you'll have two of the same endpoints, one is for the get, the other is for the post.
public class RegistrationController : Controller
{
//Get
[HttpGet]
public ActionResult RegisterHour()
{
//TODO: do anything
return View("Index");
}
//Post
[HttpPost]
public ActionResult RegisterHour(Registration newUser)
{
if(Model.IsValid)
{
//Save user to the database
userRepository.AddUser(newUser);
//load success screen.
return RedirectAction("SuccessfulRegistration");
}
//If Model is invalid handle error on the client.
return View("Index");
}
}
I am learning MVC, following THIS tutorial. (link will take you directly to where i'm stuck). so far I have learnt, there's a controller for every view. Now i have to take input from user through web entry form as mentioned in tutorial. In my project, i have a controller named Default1 and i can run it as localhost:xyz/Default1/Index. it runs perfect.
Then i created a new Controller, named Default2 and bound it to some view to display some data, and it worked perfect as localhost:xyz/Default2/Displaycustomer. the customer information was static (hard coded). and controller is as:
public ViewResult DisplayCustomers()
{
Customer cobj = new Customer();
cobj.Code = "12";
cobj.Name = "Zeeshan";
cobj.Amount = 7000;
return View("DisplayCustomers",cobj);
}
Now i have to take input from User, regarding cutomer iformation, using html page as mentioned in tutorial. so i tried adding a new webform under view folder, and and modified my controller as:
[HttpPost]
public ViewResult DisplayCustomers()
{
Customer cobj = new Customer();
cobj.Code = Request.Form["Id"].ToString();
cobj.Name = Request.Form["Name"].ToString();
cobj.Amount = Convert.ToDouble(Request.Form["Amount"].ToString());
return View("DisplayCustomers",cobj);
}
My Question is: How can i make my project stared, so that it takes input first, and then displays it, using above controller? Did i add the webform at right location? What would be the link to run it? i tried localhost:xyz/Default2/entryform etc. but failed.
(in my entryform.aspx, i have mentioned form action="DisplayCustomer" )
It sounds like what you're missing is an action to just display the form. In otherwords, you just need an action to display a form. That form's POST action should reference your controller's DisplayCustomers action.
So in your controller code:
public class CustomerController : Controller
{
[HttpGet]
public ViewResult New()
{
return View("NewCustomer"); //Our view that contains the new customer form.
}
// Add your code for displaying customers below
}
And in your view, you have code like this
#using(Html.BeginForm("DisplayCustomers", "Customer")) {
<!-- Add your form controls here -->
}
Notice that I'm using the version of the BeginForm helper that specifies the action method and controller to call. This will write the form tag to post back to your DisplayCustomers action. Here is the equivalent HTML:
<form method="POST" action="/Customer/DisplayCustomers">
You would then access your form using the URL http://test.server/Customer/New.
This may not be the best example in the world...but this will at least get you rolling..
url would be:localhost:1234/Home/Customer
the controller
public ActionResult Customer()
{
return View();
}
[HttpPost]
public ActionResult Customer(FormCollection frm)
{
var name = frm["name"].ToString();
var address = frm["address"].ToString();
ViewBag.Name = name;
ViewBag.Address = address;
return View();
}
The view
<div>
#using (Html.BeginForm())
{
<input type="text" name="name" id="name" />
<input type="text" name="address" id="address"/>
<input type="submit" name="submit" value="submit" />
<input type="text" name="namedisplay" value='#ViewBag.Name'/>
<input type="text" name="addressdisplay" value='#ViewBag.Address'/>
}
</div>