How to invoke the JavaScript function from code behind (c# / controller)? [closed] - c#

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.

Related

Call a C# method from an html a tag [closed]

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 6 years ago.
Improve this question
I have the following C# method, how can I call it from an HTML a tag?
CLICK
public void MyFunction()
{
Response.Write("<script>alert('Hello');</script>");
}
You can use jQuery ajax to MVC action:
function CallMyCShartAtion(id)
{
$.ajax({
url: 'urlToController', //path to the contoller's action
data: { id: id }
}).done(function() {
alert('Added');
});
}
And then
CLICK
http://api.jquery.com/jQuery.ajax/
This isn't directly possible (at least not in the way you're trying to do it). First of all, JavaScript is typically a client-side language, which can be run in your browser, as opposed to C# (or Java or PHP or...) which is typically run on the server side.
In order to accomplish this task like you're asking for here you need to
have some kind of backend in place (say, in C#)
issue a request from the client side (frontend) to the backend (e.g. using AJAX)
Use direct javascript for that functionality, no need to use c#
CLICK

Alternative for asp:panel in asp.net mvc [closed]

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.

How to make website readonly that link from certain websites. asp.net [closed]

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 7 years ago.
Improve this question
I have a website where a user logs in, can see his information and edit it. There is also another website, something like a site only for admins, where if I click a link it redirects me to the first website, logged in as that user but I can only read his information not edit it. I am having trouble finding out how to make website 1 both readable only and read/writeable.
I am doing this in Asp.NET mvc using C#.
One method would be to check for authorization in the Razor view.
Psuedocode:
#if(User.IsAuthorizedForEdit()
{
#*your edit view code*#
}
else
{
#*your readonly view code*#
}
This does make for some bloaty Razor. The other (arguably, better) alternative is to direct them to the appropriate view in your controller based on user.
Handy-wavy psuedocode to give you an idea:
public ActionResult ViewProfile(int profileId)
{
var user = GetCurrentUser();//without looking at your code, I can't infer this piece.
var profile = GetProfile(profileId);
if(IsAuthorizedToEdit(user, profileId)
{
return View("edit", profile);
}
else
{
return View("view", profile);
}
}
In theory, you already have a read-only view and an edit view, so the latter would be more reusable.

Increment the number of votes button [closed]

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

Handling missing and null parameters in asp.net mvc [closed]

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 new to asp.net mvc.
now the requirement is that I have to handle following URLs in same ActionResult
if HTTP//jpripu/Handset/shop_code=&hosho_date= then do something.
if HTTP//jpripu/Handset/shop_code=Cust01&hosho_date=20131212 then do something
if HTTP//jpripu/Handset/hosho_date= then do something
if HTTP//jpripu/Handset/shop_code= then do something
Is it feasible to execute above conditions separately?
could anybody help me on this. Thanks.
If you mean URLs like http://jpripu/shop_code=&hosho_date=20130923
then this controller is for you:
public class HandsetController : Controller
{
public ActionResult Index(string shop_code, string hosho_date)
{
ViewBag.shop_code = shop_code;
ViewBag.hosho_date = hosho_date;
return View();
}
}
Note: Index - is default Action, defined in your routing.
Also I suggest Pluralsight Introduction to ASP.NET MVC 3 screencasts as a quick-start quide to ASP.NET MVC.

Categories