I have two input control and and one Html.ActionLink control on my page. How do i pass the value of these input control as parameter to Controller Action.
<input id="txtName" value="MyName" type="Text"/>
<input id="txtAge" value="20" type="Text"/>
<%: Html.ActionLink("Add", "AddName", "EditProfile", new{ --- What to Pass--}, new { #Class = "openDialog", data_dialog_id = "AddCarrierDialog", data_dialog_title = "Add Carrier" })%>
My Controller take two parameter to show detail
[HttpPost]
public ActionResult AddName(string Name, int Age)
{
return PartialView("ShowData");
}
I am not able to pass txtName and TxtAge values to controller Action. Can Anyone help me out.
Thanks in Advance!!!
You will need javascript in order to achieve that. A better approach would be to use an html <form> instead of an action link as it will automatically send the values that the user entered in the input fields to the server:
<% using (Html.BeginForm("AddName", "EditProfile", FormMethod.Post, new { #class = "openDialog", data_dialog_id = "AddCarrierDialog", data_dialog_title = "Add Carrier" })) { %>
<input id="txtName" value="MyName" type="text" />
<input id="txtAge" value="20" type="text" />
<input type="submit" value="Add" />
<% } %>
Related
I have trouble with formcollection.
I have some form in view, and two submit buttons both with unique name="". On debug I get on controller in formcollection all data except "name" of submitted button... I don't know why, 'cos I use this in other forms and there it works good. So I look at the code, but I couldn't find any differencies in using formcollection on not working formcol and working formcol. I tried rename buttons, move them, add referencies which I thought that could help... Nothing. On every submit skip my condition because it give me back only "false" and formcollection my "upload" or "save" button on onclick doesn't contain...
So I would like to ask you for help with this. Can you tell me where could be an error? Thanks to all!
This is in controller:
[HttpPost]
public ActionResult EditUser(EditUserVM model, int id, FormCollection c)
{
//c["upload"] is everytime set to null, 'cos c does't contain it
if (c["upload"] != null)
{
//.... some code
return RedirectToAction("Index", "Home");
}
if (ModelState.IsValid)
{
//.... next code
}
return View("EditUser", model);
}
This is in View:
#model PrukazOnline.ViewModels.EditUserVM
#{
ViewBag.Title = "EditUser";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
#using (Html.BeginForm("EditUser", "User", null, FormMethod.Post, new { #class = "form-horizontal", id = "formstep", enctype = "multipart/form-data" }))
{
#*Here is some code - #Html.TextBoxFor(x => x.something) and #Html.ValidationMessageFor(x => x.something) - all of these are in formcollection*#
.
.
.
.
<div>
<input type="submit" value="Nahrát" class="upl" name="upload" id="upload" />
</div>
<div class="submit_buttons">
<input type="submit" name="save" id="save" value="Uložit" />
</div>
}
Issue is in multiple submit inputs in single form. There will only clicked button in FormCollection.
I suppose you try to vary form processing base on clicked button, in this case it's better to use different Actions for each button like this:
View:
<div>
<input type="submit" value="Nahrát" class="upl" name="upload" id="upload" />
</div>
<div class="submit_buttons">
<input type="submit" name="save" id="save" value="Uložit" />
</div>
Controller:
[HttpParamAction]
[HttpPost]
public ActionResult Upload(EditUserVM model)
{
...
}
[HttpParamAction]
[HttpPost]
public ActionResult Save(EditUserVM model)
{
...
}
Hi all I have a view with these controls on it
<input type="text" class="radius2" />
<input type="button" value="Place bid" class="bid-btn" />
I want to pass the value in the text box to another view (via an action method) when the button is clicked. The action method should render the view.
How can this be acheived?
Thanks,
Sachin
#using(Html.BeginForm("SubmitAction", "Home", FormMethod.Post))
{
<input id="radius2" name="radius2" type="text" class="radius2" />
<input type="submit" value="Place bid" class="bid-btn" />
}
[HttpPost]
public ActionResult SubmitAction(string radius2)
{
return AnotherView(radius2);
}
public ActionResult AnotherView(string value)
{
return View();
}
Written in notepad so might require small syntax changes
add name property to the tags so the server can access them
<input name="Name1" type="text" class="radius2" />
<input type="button" value="Place bid" class="bid-btn" />
then when submit your action should be something like this
public ActionResult SubmitAction(string name1)
{
return View("ViewName" , /* the model here */ )
}
I am using the partial view with the ajax.beginform. In that partial view page, i have the following markup
EDIT
<%
using (Ajax.BeginForm("ManageDataSources", "DataSources", saveAjaxOptions))
{
%>....
<td>
<%: Html.Hidden("DataSource_Id", dataSource.Id, new { #class = "DataSource_Id" })%>
<%: Html.TextBox("DataSource_Name", dataSource.Name, new { #class = "DataSource_Name" })%>
</td>
<tr class="queryParameters" style="display: block">
<td colspan="2" align="center">
<input id="Text1" name="parametername" type="text" />
<input id="Text2" name="parametervalue" type="text" />
<input id="Text3" name="parametername" type="text" />
<input id="Text4" name="parametervalue" type="text" />
<input id="Text5" name="parametername" type="text" />
<input id="Text6" name="parametervalue" type="text" />
<input id="Text7" name="parametername" type="text" />
<input id="Text8" name="parametervalue" type="text" />
<input id="Text9" name="parametername" type="text" />
<input id="Text10" name="parametervalue" type="text" />
</td>
</tr>
and in the the controller, i have this model for the representation of the data
public class DataSourceViewModel
{
public string DataSource_Id { get; set; }
public string DataSource_Name { get; set; }
public List<SCParams> parameters { get; set; }
}
public class SCParams
{
public string parametername { get; set; }
public string parametervalue { get; set; }
}
EDIT
public ActionResult ManageDataSources(DataSourceViewModel dsvm)
{
return PartialView("ManageDataSources");
}
when i post the data these parametername and parameter values are not at all bound to the list of objects. How do i do this. i am using microsoft ajax and want to do this without using other plugings. Kindly suggest the right way.
EDIT
This is the data in the header taken from chrome
DataSource_Id:
DataSource_Name:Name
parametername:a
parametervalue:1
parametername:q
parametervalue:2
parametername:z
parametervalue:3
parametername:s
parametervalue:4
parametername:w
parametervalue:5
x:15
y:12
What I understand you have master detail structure and you want to receive it controller. if that is the case. then there are two possibilities either your detail portion has variable length detail portion or fixed length detail portion. You may follow the post here for variable length as well as fixed length. For Fixed length you may also follow here.
You will receive the model in following signature
public ActionResult ManageDataSources(DataSourceViewModel dsvm)
moreover you may also check formcollection parameter for actionresult
[HttpPost]
public ActionResult MyAction(FormCollection collection)
How does one obtain the form data after submitting it?
<form target="_self" runat="server">
<p>
<select id="BLAHBLAH2">
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
</select>
<input type="submit" runat="server" value="Change Year" />
</p>
</form>
This hits the controller's Index method. But, there's nothing in Request.Form. Why?
Second, can I use
<input type="button" instead of type=submit? That is, without introducing ajax via onclick.
Finally, how do I submit to a different method in the controller, e.g. Create?
Try removing those runat server tags. They should not be used in ASP.NET MVC. Also your select doesn't have a name. If an input element doesn't have a name it won't submit anything. Also your option tags must have value attributes which indicates what value will be sent to the server if this options is selected:
<form action="/Home/Create" method="post">
<p>
<select id="BLAHBLAH2" name="BLAHBLAH2">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
<input type="submit" value="Change Year" />
</p>
</form>
But the correct way to generate forms in ASP.NET MVC is to use HTML helpers. Depending on the view engine you are using the syntax might be different. Here's an example with the Razor view engine:
#model MyViewModel
#using (Html.BeginForm("Create", "Home"))
{
<p>
#Html.DropDownListFor(x => x.SelectedYear, Model.Years)
<input type="submit" value="Change Year" />
</p>
}
Here you have a strongly typed view to some given view model:
public class MyViewModel
{
public string SelectedYear { get; set; }
public IEnumerable<SelectListItem> Years
{
get
{
return Enumerable
.Range(2010, 4)
.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = x.ToString()
});
}
}
}
which is populated by some controller action that will render this view:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Create(MyViewModel model)
{
... model.SelectedYear will contain the selected year
}
}
None of your <option> tags have a value:
...
<option value="2010">2010</option>
...
As noted by David, runat="server" is most definitely a WebForms thing, so you can 86 that.
If you want to submit to a different method on your controller you just need to specify the URL for that method.
Easy way using Html.BeginForm:
#using (Html.BeginForm("AnotherAction", "ControllerName")) {
<!-- Your magic form here -->
}
Using Url.Action
<form action="#Url.Action("AnotherAction")" method="POST">
<!-- Your magic form here -->
</form>
You can also use
In Controller
int Value = Convert.ToInt32(Request["BLAHBLAH2"]); //To retrieve this int value
In .cshtml file use
<select id="IDxxx" name="BLAHBLAH2">
//Request[""] will retrieve the VALUE for the html object ,whose "name" you request.
I have a page that contains multiple inputs - I'm using Ajax.BeginForm to build a form for each set of inputs.
<% using (Ajax.BeginForm(new AjaxOptions() { InsertionMode = InsertionMode.InsertAfter, HttpMethod = "POST" }))
{ %>
<input class="smallInput" type="text" name="duration"/>
<input type="submit" value="Add" />
<% } %>
The controller looks like this
[AcceptVerbs("POST")]
public ActionResult AddExercise(FormCollection form)
{
// some save logic...
return Content(string.Empty);
}
This works - the data is submitted to my controller and is saved.
However, every time this happens my page is replaced by a blank page. If I explicitly return a View from my Action, that view appears instead. But I want it to submit the form, leaving my existing page in place as-is.
I wasn't including the MVC Ajax javascript files in my page. Adding those includes fixed the problem.
You need to specify the id of the DOM element that you want to be updated:
<% using (Ajax.BeginForm(new AjaxOptions() {
UpdateTargetId = "someID",
InsertionMode = InsertionMode.InsertAfter,
HttpMethod = "POST" }))
{ %>
<input class="smallInput" type="text" name="duration"/>
<input type="submit" value="Add" />
<% } %>