简体   繁体   中英

razor mvc4 and c# codebehind

Alright, so I have just started with razor mvc4 and I have a little experience with c#. I am currently making a website on which there is a button. my html follows:

<button onclick ="vote1_click"  id="VoteButton" value="Vote">Vote</button>

this is in a .cshtml view

I then have a class to handle the vote1_click event. It is in c# and follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1
{
    public class voting
    {
        public void vote1_click(object sender, EventArgs e)
        {
        }
    }
}

I believe that my issue is a fundamental understanding of the razor structure, but could not figure it out on my own.

Any help at all is appreciated, and I will try to not feel too stupid when the answer is simple.

Thanks!

EDIT:

I have been getting an issue where the Add(string name) gives me an error of "not all code paths return a value"

here is the rest of my code as requested:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;

namespace WAgermanClub.Controllers
{
public class HomeController : Controller
{

    [HttpPost]
    public ActionResult Add(string vote)
    {




        SqlConnection vote1connection = new SqlConnection("user id=userid;" +
                                      "password=validpassword;server=o5z5dpwpzi.database.windows.net;" +
                               "Trusted_Connection=yes;" +
                               "database=wagermanclub_votes; " +
                               "connection timeout=30");
        try
        {
            vote1connection.Open();
        }
        catch (Exception g)
        {
            Console.WriteLine(g.ToString());
        }

        try
        {
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from table", vote1connection);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {

                Console.WriteLine(myReader["Vote1"].ToString());
            }
        }
        catch (Exception i)
        {
            Console.WriteLine(i.ToString());
        }


        SqlCommand vote1command = new SqlCommand("INSERT INTO table (Column1, Vote1) " +
                              "Values (1, 'Vote1' + 1)", vote1connection);


        vote1command.ExecuteNonQuery();

        try
        {
            vote1connection.Close();
        }
        catch (Exception h)
        {
            Console.WriteLine(h.ToString());
        }






    }
}

}

And here is my HTML:

@{
ViewBag.Title = "Ideas";

}

 @section featured {
<section class="featured">
    <div class="content-wrapper">
        <hgroup class="title">
            <h1>@ViewBag.Title.</h1>
            <h2>@ViewBag.Message</h2>
        </hgroup>
        <p>

        </p>
    </div>
</section>
}

<body>
<div  style="border: solid; max-width: 300px; margin-left: auto; margin-right: auto">


    @using(Html.BeginForm())
    {

    <input type="submit" value="Vote"/>
    }

 </div>




</body>

Thanks!

You are confused with ASP.NET webforms and MVC. MVC works more in the classic Web (GET-POST form) style. You post a form with values. There is no such click events and event handler in codebehind like what you have in web forms.

So to render your page, you may have an action method like this in your HomeController

public class HomeController : Controller
{
   public ActionResult Add()
   {
     return View();
   }
}

So in your Add view(razor file), you need to have some code to render a form tag with input elements. Let's use the Html.Begin form helper method to render the form tag for us.

@using(Html.Beginform())
{    
  <input type="text" name="name" />
  <input type="submit" />
}

This will render a form tag in your markup with action property set as " Home/Add ", assuming your GET action method is in HomeController. (Check the view source of the page)

So when user clicks on the submit button it will post the form to the Add action. so make sure you have an action method like this in HomeController to handle the form posting.(The one decorated with HttpPost attribute

[HttpPost]
public ActionResult Add(string name)
{
  //do something with the posted values
  return RedirectToAction("Success");  // redirecting to another view
}

You may be confusing the webforms model with the asp.net mvc model.

razor is only available for you when using webpages or asp.net mvc.

For asp.net mvc there's not concept of a server method/event as you've defined here.

You'll typically need to define action methods in your controllers which will be responsible for processing whatever forms your are posting.

You may want to check out more on ASP.NET MVC

MVC does not implement the web forms style viewstate and event handlers. So there is no vote1_click. What you would want to do is either

1) Create a JavaScript Post/Get back to the server

or

2) have a form and post back all the form variables back to the server

Here is a pretty good example of beginning MVC: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM