Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to call a controller method on a button click. I created a java function for this, and now I'm stuck...
using (Html.BeginForm("Create", "Packaging", FormMethod.Post))
{%>
......
<input type="submit" value="Save" id="btnSave" onclick="return btnSave_onclick()" />
}%>
function btnSave_onclick() {
window.open(Url.Action("Index","Packaging"));
}
Thanks in advance.
Try this
window.location.href = "#Url.Action("Index", "Packaging")" + "/";
It looks like you are trying to redirect the user to the Index action of the Packaging controller once details have been created.
You do this on the server side inside the Create action on the Packaging controller by return the following:
return RedirectToAction("Index");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In MVC, Controller - I will have to enable or disable the text box in the page load event.
Mentioned line of syntax is written inside a JS file / function.
document.getElementById('ShowTextBox').disabled = false;
How to invoke the JavaScript function from code behind (c# / controller)?
Arg, too many comments:
if you have a simple need to send some result of server side processing to your View, one time (onload), then you have ViewBag and/or ViewData
Ajax/XHR for more processing
even partials if you ever have to (partial views with javascript mixed in with c# or vb stuff)
<textarea id="ShowTextBox" asp-for="ShowTextBox" style="height:150px;" class="form-control"></textarea>
Trivial sample only improve as needed:
Controller:
public ActionResult Index()
{
//some server side processing....
ViewBag.Foo = DateTime.Now.Second % 2 == 0;
return View();
}
View (index.cshtml):
<textarea id="ShowTextBox" asp-for="ShowTextBox" style="height:150px;" class="form-control"></textarea>
<script>
// trivial example only
document.getElementById('ShowTextBox').disabled = #ViewBag.Foo;
</script>
REF:
Passing data to views - doc is for Core, but same concept.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am coding in c# asp.net using razor technology, I am still getting errors in designing a button that each click considered as vote , and I should display that! But i cant get it in a right logic :(
Here is my code in the view
using (Html.BeginForm("OpenBidPanelOnItem", "Home", FormMethod.Post))
{
<button name="Vote" value="Vote" style="background-color: green; width: 60px">
<b style="color:snow">Vote</b>
</button>
<h3>Total Votes for #registerMember.UserName is : #Model.NumberOfVotes votes</h3>
}
Where should I increment the number of votes by 1.
You will have a OpenBidPanelOnItem action on your HomeController and persist the change to datastore for next render. It would look something like this in C#.
public ActionResult OpenBidPanelOnItem() {
var model = GetModelFromSomewhere();
model.NumberOfVotes++;
SaveModelToPersistentDataStore(model);
}
Your button I assume will need to be your submit button so add type="submit" to the button attributes.
Hope this helps and makes sense.
Gareth
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to pass a model's value from view to controller.
When I use #Html.TextBoxFor, the value gets to the controller, but when I use #Html.DisplayFor, the value is null in the controller.
View.cshtml
#using (Html.BeginForm("EditView", "Home"))
{
#Html.TextBoxFor(model => model.Name) // 1st way
#Html.DisplayFor(model => model.Name) // 2nd way
<input type="submit" value="Ok !" class="btn btn-success btn-sm" />
}
Controller
public ActionResult EditView(ModelList model)
{
return RedirectToAction("Index");
}
Basically, the 1st time the controller gets the value; but the 2nd time, it gets null. How can I resolve this?
#Html.DisplayFor just displays the value in the view.
If you want the value to be passed back to the controller on post then you also need a #Html.HiddenFor
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using hidden type form value in HTML. how we connect this with javascript and jquery.
I tried this:
HTML :
<label>Company Name:</label><br />
<input type="text" name="21602-0" id="21602-0" size="30" value="<%= FormValues["21602-0"] % />
JS:
vars[1] != null ? $('#21620-' + index.toString()).val(vars[1]) : $('#21620-' + index.toString()).val('');
Hidden fields are those fields as their name suggest which are not visible on UI (but available in source html), you can maintain state of some element using these field.
Now to access these field by name using jQuery try:
var text = $('[name="ElementNameHere"]').val();
and to set field value:
$('[name="ElementNameHere"]').val('new Text');
and using JavaScript:
var text = document.getElementsByName('ElementNameHere').value;
//or set new text
document.getElementsByName('ElementNameHere').value= 'new text';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have simple form with Post request to page. When user clicks the subscribe button he comes to the page, where I would like to have information about where it come from. I have tested it using small site, to make sure I have redirect..
Request.UrlReferrer and ServerVariables["HTTP_REFERER"] is NULL
IP and all other data is in place.
How do i find the Url, Form was posted to my page from ?
Form designed to be embedded on any site/page with simply copy/paste.
Thanks.
Another SO user has reported that HTTP_REFERER doesn't get populated in posts across domains.
So I think it would be better to rely on JavaScript to populate a hidden input in your form with the referring page just before submission. Something like this:
<form action="http://www.yourdomain.com/subscribe"
method="POST"
onsubmit=
"document.getElementById('www.yourdomain.com.referrer').value=window.location;" >
<!-- hidden input for field starts with a domain registered by you
just so that it's unlikely to clash with anything else on the page -->
<input type="hidden" id="www.yourdomain.com.referrer" name="referrer"/>
your email: <input name="email" type="text"/>
... rest of form ...
<input type="submit" value="Subscribe"/>
</form>
Then you can get the referring URL from Request.Form["referrer"] in the handling page.