简体   繁体   中英

How to set two models in single view in MVC 5 C#?

I am new to MVC framework. I saw some of the links of stack overflow, but I didn't get the point.

I tried the below code, could anyone tell me, what's wrong with that?.

I create 3 models

1.SignIn.cs

public class SignIn
{
    public string userName { get; set; }
    public string password { get; set; }

    public string getSignIn(string name,string pword)
    {
        if(name.equals('test') && pword.equals('xxx'))
        {
            return "correct";
        }
        else
        {
            return "Failed";
        }
    }
}

2.SignUp

public class Signup
{
    public string name { get; set; }
    public string emailId { get; set; }
    public string mobileNo { get; set; }
    public string password { get; set; }

    public string setUser(string profileName, string email,string mobile,string pword)
    {
        var cs = ConfigurationManager.ConnectionStrings["csTest"].ConnectionString;
        var outputValue = "";
        try
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                //Database code
            }
            return outputValue;
        }
        catch(Exception ee)
        {
            return ee.ToString();
        }
    } 
}

3.vwSignINAndSignUP

public class vwSignINAndSignUP
{
    public SignIn getSignin { get; set; }
    public Signup setSignUp { get; set; }
}

HomeController

public ActionResult IndexLoginRegister()
{
    Models.vwSignINAndSignUP vmSign = new Models.vwSignINAndSignUP ();
    string uname = vmSign.getSignin.userName;
    string pword = vmSign.getSignin.password;

    //NOTE: For testing, I am directly printing using below code, otherwise I will pass to SignIn Model to verify
    return Content("Username = "+uname+", Password = "+pword);
}

I am trying to separate SignIn and Signup in a single view.

Index.cshtml

@using TestMVC.Models
@model vwSignINAndSignUP 

@using (Html.BeginForm("IndexLoginRegister", "Home",FormMethod.Post))
{
    @Html.TextBoxFor(si=> si.getSignin.userName,new {@class="form-control",@id="txtLogin" })
    @Html.TextBoxFor(si => si.getSignin.password, new { @class="form-control",@id = "txtPassword", @type="password"})
    <input type="submit" class="btn btn-sm btn-primary btn-rounded" value="Login" id="btnLoginSubmit" />
}

@using (Html.BeginForm("IndexLoginRegister", "Home",FormMethod.Post))
{
    @Html.TextBoxFor(su => su.setSignUp.name, new { @class = "form-control", @id = "txtName" })
    @Html.TextBoxFor(su => su.setSignUp.emailId, new { @class = "form-control", @id = "txtEmailId" })
    @Html.TextBoxFor(su => su.setSignUp.mobileNo, new { @class = "form-control", @id = "txtMobileNo" })
    @Html.TextBoxFor(su => su.setSignUp.password, new { @class = "form-control", @id = "txtPassword", @type = "password" })

    <input type="submit" class="btn btn-sm btn-primary btn-rounded" value="Signup" id="btnSubmit" />
}

ERROR

when I try to login, it shows following error

System.NullReferenceException: Object reference not set to an instance of an object.

string uname = vmSign.getSignin.userName;

Because you post miss get method, here are code for your requirement. You need have a GET and a POST action, post action should have model as parameter.

public class HomeController : Controller
    {
        public ActionResult IndexLoginRegister()
        {
            return View("Index");
        }

        [HttpPost]
        public ActionResult IndexLoginRegister(vwSignINAndSignUP vmSign)
        {
            //Models.vwSignINAndSignUP vmSign = new Models.vwSignINAndSignUP();
            string uname = vmSign.getSignin.userName;
            string pword = vmSign.getSignin.password;

            //NOTE: For testing, I am directly printing using below code, otherwise I will pass to SignIn Model to verify
            return Content("Username = " + uname + ", Password = " + pword);
        }
    }

To prevent error you need init properties

public class vwSignINAndSignUP
    {
        public SignIn getSignin { get; set; } = new SignIn();
        public Signup setSignUp { get; set; } = new Signup();
    }

In controller change

return Content("Username = " + uname + ", Password = " + pword);

to

return View("Index", vmSign);

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