-2

I am trying to get data from a textbox in a form inside view to controller in asp.net-mvc. My requirement is to compare Token_No. with some id and Password with some existing password. How to get get those values at controller side on button click inside if condition. I am not using razor syntax so how to accomplish this situation without using razor syntax and strongly bind with model.

Code in view

<form name="ctl00" id="ctl00" action="HomeController/Index" method="post" data-dpmaxz-fid="1">
    <div class="input-group">
        <span class="input-group-addon"> <i>Token_No.</i></span>
        <div class="form-group is-empty">
            <input name="txtToken" class="form-control" id="txtToken" type="text" placeholder="Token No..." data-dpmaxz-eid="5">
            <span class="material-input"></span>
        </div>
    </div>
    <div class="input-group"> <span class="input-group-addon"> 
        <i class="material-icons">lock_outline</i> </span>
        <div class="form-group is-empty">
            <input name="txtPswd" class="form-control" id="txtPswd" type="password" placeholder="Password..." data-dpmaxz-eid="5">
            <span class="material-input"></span>
        </div>
    </div>
    <div class="footer text-center">
        <input name="btn_Login" class="btn btn-primary" id="btn_Login" type="submit" value="Login" data-dpmaxz-eid="6">
    </div>
</form>

Code in Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Index(EmpDetails model)
    {
        if (model.token_no.="197418")
        {

        }
        return View();
    }
}
micky
  • 23
  • 7
  • Your not generating any form control for a property named `token_no`. Always use the strongly typed `HtmlHelper` methods to correctly generate your form controls –  Sep 20 '17 at 05:32
  • @StephenMuecke how to generate form control can you suggest i dont have any idea about that. – micky Sep 20 '17 at 05:37
  • At the top of the view - `@model yourAssembly.EmpDetails` and then `@using (Html.BeginForm()) { @Html.TextBoxFor(m => m.token_no) ....` etc. You need to go to the mvc site and work through the tutorials to learn the basics –  Sep 20 '17 at 05:41

1 Answers1

0

you can generate the view using controller method & create a strongly typed view. then you can choose a model class & select scaffold template as what you want..you can select create,edit,delete..etc as you want.then you can access the text box values using form collection.

    [HttpPost]
    public ActionResult authenticateUser(FormCollection formCollection)
    {
        int token = int.Parse(formCollection["txtToken"]);
        string pswd = formCollection["txtPswd"];
        return View();
    }
Chinmak
  • 26
  • 4
  • Thx .chinthaka. one more doubt is how to redirect to a new view page if we passed if condition .i am trying like this but error is at 'Index' (i.e not all code path return a value. public ActionResult Index(FormCollection formcollection) { string txtTok = formcollection["txtToken"]; string pswd = formcollection["txtPswd"]; if ((txtTok == "197418") && (pswd == "123456")) { return RedirectToAction("About"); } } – micky Sep 20 '17 at 06:25
  • this will help..https://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value – Chinmak Sep 20 '17 at 06:36
  • thx i got this .but i want to redirect to a new webpage inside view folder e.g"About.cshtml" so how to do that. – micky Sep 20 '17 at 06:45
  • if you want do so, first you have to create a method named "About" & create a view for that method.then you can redirect to a "About.cshtml" page – Chinmak Sep 20 '17 at 06:57