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
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 3 years ago.
Improve this question
I have a text in SQL like: "I want to eat. I'm hungry."
I use ASP.NET MVC to get data from database:
#Model.content
#Model.content is "I want to eat. I'm hungry."
I want to format text to display, for example, it might come down the line:
I want to eat.
I'm hungry.
Since, you don't prefer saving "\n" in the database, you can just split the entire into an array and loop through each item to display it with breaks. use Split
Something like:
#foreach (var item in Model.content.Split(".")) { #item <br /> }
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Now that every thing is being converted from the conventional aspx to razor view because of its benefits,
my question is how to achieve thing similar to panel, where in we were allowed that if X is true, show the panel else visible = false.
How can we achieve a similar thing in MVC?
You can do it with razor in the view:
#if(condition) {
<div>............ </div>
}
The panel is actually <div id="yourdivid">...</div> element in MVC view, thus you can either process condition directly on view as Matteo1010 said, or use JS if you want to setup views with only passed values and HTML helpers:
<script type="text/javascript">
var condition = '#[passed condition value here]';
if (condition) {
// show panel
document.getElementById("yourdivid").style.visibility = "visible";
}
else {
// hide panel
document.getElementById("yourdivid").style.visibility = "hidden";
}
</script>
<div id="yourdivid"></div>
I think JS approach with passed value has more advantage to control the view behaviour in client-side than write C# code directly inside the view.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've got a large .net form with hundreds of input fields, sometimes users navigate off the form page without saving, what's the best way to check if a field value has changed when they try to navigate away? some c# function or javascript?
Don't take the control out of the users hand :-)
Ask him on exit, if he wants to save the changes. Maybe he did some changes by mistake, which you don't want to save.
You can achieve this by Javascript/Jquery. Something like:
$(document).ready(function() {
formmodified=0;
$('form *').change(function(){
formmodified=1;
});
window.onbeforeunload = confirmExit;
function confirmExit() {
if (formmodified == 1) {
return "New information not saved. Do you wish to leave the page?";
}
}
$("input[name='commit']").click(function() {
formmodified = 0;
});
});
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");