ASP.NET Saving Data - c#

So I have a basic crud that displays information on ASP.Net using entity framework. When i click details it shows the information for that row on a table. I've input a button and a label that when is clicked shows a number, when clicked again it will show the next number higher. It's basically to just a button counter written with JQuery. My question is, is there a way to save this number? I want to store the data in the program so it doesn't forget the number when I move to a different page.
Here is the JQuery.
var count = 0;
function Count() {
count++;
$('#lblShow').text(count);
}
<div>
<input type="submit" name="btnCount" value="Add Signature" onclick="return Count();" id="btnCount" /> <input type="submit" name="btnCountSoc" value="Post to Social Media" id="btnMedia" />
<span id="lblShow"></span>
</div>
Any advice would be appreciated, thanks.

You can use cookies like :
var counter = $("#foo").val();
document.cookie = "counter="+counter";path=/";

Related

CSHTML displaying user inputs

My web assignment has asked me to create a dating site login page and display the information, i have completed the HTML to a point and am now beginning with the C#. I have began this but have come to a standstill as I dont know what is going wrong to display the information.
My question is: How do I get the "fName" to display itself in the "message" as when I submit it, the text comes up but there is no name when I type it into the textbox and submit it.
C#
#{
string message = "";
string fName;
fName = Request.Form["fName"];
string daySelect = Request.Form["submit"];
message = String.Format("Your first name is: {0}", Request.Form["fName"]);
}
HTML
<form method="post">
<label for="fName"></label>
First Name: <input type="text" name="fName" id="fName"
#(Request.Form["fName"]) /> <br/>
<br/><br/><input type="button" value="submit" />
<p>#message</p>
I recommend to look into ASP.NET MVC and how are models passed from controller to their views. This makes things you are doing here a lot of easier and less error prone. There are several tutorials out there.
Just looking at your HTML code a missing value attribute in the input field may be the cause. Please try
First Name: <input type="text" name="fName" id="fName" value="#(Request.Form["fName"])" /> <br/>
I found the problem.
Here is the submit that I have in:
<br/><br/><input type="button" value="submit" />
As you can see the type is button, for this code it should be of type "submit", tiny error in lots of code.
Thanks to rboe for the assistance.

Reddit bot - either bypass or click "yes" to over 18 check?

I am making a Reddit comment bot that will crawl subreddits as it finds links in the pages. My problem is that when I try to crawl NSFW subreddits with an 18+ age question, despite clicking "yes" in my normal browser, Reddit returns a 18+ age question every time the C# bot hits those pages.
Is there a way to avoid this or to click the "yes" button programatically?
yes there is a way, look at the HTML form for the question:
<form method="post" action="" class="pretty-form">
<input type="hidden" name="uh" value="">
<p>are you over eighteen and willing to see adult content?</p>
<p>
<button type="submit" name="over18" value="yes">yes</button>
<button type="submit" name="over18" value="no">no</button>
</p>
</form>
You could fill out the values and then submit the POST request.

How to disable angularjs from capturing my form submit for the second submit button

Thanks for checking.
I am building a web application using angularjs. i have two buttons one will do a Ajax post to server using angularjs and the other will to a normal html post to server. the reason i want the second one to do a direct post to server is that i want to generate an excel document on server and return it tp user to download on response.
please how can i disable angularjs from capturing my form submit for the secont submit button
I'd say that the simplest way is to change the button that shouldn't post the form to not be a form button, but another element that doesn't have that default behaviour. That is, change this:
<form>
<input type="text">
<input type="submit" ng-click="doStuff()" value="Ajaxy things">
<input type="submit" value="Real post">
</form>
to:
<form>
<input type="text">
<a ng-click="doStuff()">Ajaxy things</a>
<input type="submit" value="Real post">
</form>
And style the a to look like a button.

How to get array values in Controller from Form Post

I have an attendance system in which the user enters the manual entry for attendance . I dont want to do a single entry and post the form.
The user will enter the Employee ID and then the Date and the time when he attended the office.
The the user will press the Add button it will not be added in the database but it will collect the values in input boxes like this
<input type="text" name="EMP_ID" value="">
<input type="text" name="DateTime" value="">
<input type="Submit" value="Add">
As soon it press the Add button the jquery will append the div box with the form elements
like this
<div class="appendBox">
<input type="text" name="time[emp_id][1][timein]" value="2014-02-02 2:00"/>
<input type="text" name="time[emp_id][1][timeout]" value="2014-02-02 2:00"/>
<input type="text" name="time[emp_id][1][Manual]" value="true"/>
</div>
when all the entries added then the submit button will appear and when submit is pressed it will get the array . I have done this in php ..
I want to achieve this in Asp.net MVC3 .
I want to create a model and fetch the values and insert it in the database
I am confused how to achieve . it
I think this is what you are looking for:
model binding to a list
also duplicate of this: Post array in ASP.NET MVC 2 using name="array_name[]" for form elements

Does ASP.NET MVC swallow submit values?

I have a single form in ASP.NET MVC (v1) that has 2 input buttons. Each submit button has to be contained within this single form and I need to know which one the user pressed.
I know about the trick to check the FormCollection values which will be returned based on the button pressed. For example, if I have and and the user clicks Button2, I should be able to say Request.Form["Button2"] != null and that will evaluate to true in which case I know that the user clicked that button.
However, this is not working for me. The values of all my buttons is null as non of them are contained within the Request.Form values. Is there a bug in ASP.NET MVC which swallows these values?
Here is my form code:
<% using (Html.BeginForm()) {%>
<% Html.RenderPartial( "EditAreaControl", Model ); %>
<div class="form-layout-command-container">
<div class="form-layout-command-area-alpha"><button type="submit" name="submit1" value="Save">Save</button></div>
<div class="form-layout-command-area-alpha"><button type="submit" name="submit2" value="SaveAndCopy">Save and Create Copy</button></div>
<div class="form-layout-command-area-beta"><%= Html.ActionLink("Cancel", "list") %></div>
</div>
<% } %>
Here is my controller code:
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Add(FormCollection values )
{
if (values["submit1"] != null)
// always false
if (values["submit2"] != null)
// always false as well
}
From w3schools:
Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the and tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.
It seems that this is not standardized. You should stick to
<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="Cancel" />
I would use inputs of type submit instead of buttons. Non-inputs may not passed back in a form post or at least can be passed back inconsistently. Note that they can have the same name with different values so that you can use the same parameter for any button that submits the form.
<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="SaveAndCopy" />
public ActionResult Save( string submitButton, ... )
{
if (submitButton == "Save")
{
...
}
else if (submitButton == "SaveAndCopy")
{
...
}
....
}
Using Firebug, I found that the submit buttons were not being sent in the response and because of that, there isn't much I can do on the MVC side. I decided to use a client side hack to populate a hidden input field on the client side which would be passed to the controller values.
I changed the input buttons to be:
<input type="submit" value="Save" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="submit" value="Save and Copy" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="hidden" id="submitAction" name="submitAction" />
The jquery script simply copies the values:
Actions.prototype.copyValues = function(from, to) {
$(to).val($(from).val());
};
The controller action then looks for the hidden input values:
var request = HttpContext.Request;
return request.Form["submitAction"];
This solves the issue from above but I realize it is not that clean.
Put them in two different forms and you will know which one submitted based on which action was called on the controller.

Categories

Resources