now how to get data back in View from model ? my code:
现在如何从模型中查看数据?我的代码:
View:
@using (Html.BeginForm("register","Home", FormMethod.Post, new {id="submitForm"}))
{
<div>
<i>@Html.Label("Name:")</i>
@Html.TextBox("txtboxName")
</div>
<div>
<i>@Html.Label("Email:")</i>
@Html.TextBox("txtboxEmail")
</div>
<div>
<i>@Html.Label("Password:")</i>
@Html.Password("txtboxPassword")
</div>
<div>
<button type="submit" id="btnSubmit" name="Command" value="Submit">Submit</button>
</div>
}
controller:
[HttpPost]
public ActionResult register(string command, FormCollection formData )
{
if (command == "Submit")
{
var name = formData["txtboxName"];
var email = formData["txtboxEmail"];
var pwd = formData["txtboxPassword"];
database db = new database();
db.connectDB(name, email, pwd);
ViewBag.Message = email;
}
return View();
}
}
}
Model:
namespace LoginSys.Models
{
public class database
{
public void connectDB(String name, String email, String pwd)
{
String conStr = "Data Source=HUNAIN-PC;Initial Catalog=registration;User ID=sa;Password=abc123!@#";
sqlconnection sqlCon = new sqlconnection(conStr);
string comm = "insert into tblRegister values('"+name+"','"+email+"','"+pwd+"')";
sqlcommand sqlCom = new sqlcom(comm, con);
try
{
sqlCon.Open();
sqlCon.executeNonQuery();
}
catch(exception exc)
{
//code
}
finally
{
con.close();
}
}
}
}
now how to return NO OF ROWS EFFECTED BY EXECUTENONQUERY statement or suppose any thing to return ? simple words and please help me in this approach, will use advance techniques later.
现在如何返回由EXECUTENONQUERY声明影响的NO行或假设有什么东西返回?简单的话,请在这种方法中帮助我,以后会使用先进技术。
1 个解决方案
#1
0
If you want to return something, you can use SqlParameter ParameterDirection.Output like this:
如果要返回某些内容,可以使用SqlParameter ParameterDirection.Output,如下所示:
SqlCommand cc = new SqlCommand(connStr); // connection string
cc.CommandText = "insert into tblRegister (Name, Email, Pwd) values (@name, @email, @pwd); select @result = @@identity";
cc.AddWithValue("@name", name);
cc.AddWithValue("@email", email);
cc.AddWithValue("@pwd", pwd);
cc.Parameters.Add("@result", SqlDbType.Int);
cc.Parameters["@result"].Direction = ParameterDirection.Output;
cc.ExecuteNonQuery();
// returs identity key
int id = (int)cc.Parameters["@result"].Value;
Then you can return this id from your function, but you have to modify it's prototype:
然后你可以从函数中返回这个id,但你必须修改它的原型:
public int connectDB(String name, String email, String pwd) // changed void to int
{
// ...
return id;
}
Then you can simply put this value to ViewBag, or use other ways to pass it to your view:
然后,您只需将此值放入ViewBag,或使用其他方式将其传递给您的视图:
database db = new database();
ViewBag.Id = db.connectDB(name, email, pwd);
ViewBag.Message = email;
If you use ViewBag, you can access it in your view with @ViewBag.Id
如果您使用ViewBag,则可以使用@ ViewBag.Id在视图中访问它
#1
0
If you want to return something, you can use SqlParameter ParameterDirection.Output like this:
如果要返回某些内容,可以使用SqlParameter ParameterDirection.Output,如下所示:
SqlCommand cc = new SqlCommand(connStr); // connection string
cc.CommandText = "insert into tblRegister (Name, Email, Pwd) values (@name, @email, @pwd); select @result = @@identity";
cc.AddWithValue("@name", name);
cc.AddWithValue("@email", email);
cc.AddWithValue("@pwd", pwd);
cc.Parameters.Add("@result", SqlDbType.Int);
cc.Parameters["@result"].Direction = ParameterDirection.Output;
cc.ExecuteNonQuery();
// returs identity key
int id = (int)cc.Parameters["@result"].Value;
Then you can return this id from your function, but you have to modify it's prototype:
然后你可以从函数中返回这个id,但你必须修改它的原型:
public int connectDB(String name, String email, String pwd) // changed void to int
{
// ...
return id;
}
Then you can simply put this value to ViewBag, or use other ways to pass it to your view:
然后,您只需将此值放入ViewBag,或使用其他方式将其传递给您的视图:
database db = new database();
ViewBag.Id = db.connectDB(name, email, pwd);
ViewBag.Message = email;
If you use ViewBag, you can access it in your view with @ViewBag.Id
如果您使用ViewBag,则可以使用@ ViewBag.Id在视图中访问它