如何在视图元素中获取模型值 [英] how to get model values in view elements

查看:74
本文介绍了如何在视图元素中获取模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器是: -



[HttpPost]

公共ActionResult注册(StudentsModel mod,FormCollection frm)

{

StudentsModel model1 = new StudentsModel();

dbStudentsInfoEntities obj = new dbStudentsInfoEntities();

tbl_StudentRegistration tbl = new tbl_StudentRegistration ();

string seach = Convert.ToString(frm [myHiddenText]);

if(seach ==Save)

{

tbl.rollNumber = mod.RollNumber;

tbl.Name = mod.StudentsName;

tbl.City = mod.City;

tbl.Address = mod.Address;

obj.tbl_StudentRegistration.AddObject(tbl);

obj.SaveChanges();

var data =来自obj.tbl_StudentRegistration中的n

selec tn;

ViewBag.Collection = data;



}

else if(seach ==Search )

{



var rno = mod.RollNumber;

var data1 =来自obj.tbl_StudentRegistration中的n

其中n.rollNumber == rno

选择n;



foreach(数据1中的var d)

{

model1.RollNumber = d.rollNumber;

model1.StudentsName = d.Name;

model1。 City = d.City;

model1.Address = d.Address;



}



}



返回查看(注册,模型1);

}

我的模特是:

My Controller is :--

[HttpPost]
public ActionResult Registration(StudentsModel mod,FormCollection frm)
{
StudentsModel model1 = new StudentsModel();
dbStudentsInfoEntities obj=new dbStudentsInfoEntities();
tbl_StudentRegistration tbl = new tbl_StudentRegistration();
string seach = Convert.ToString(frm["myHiddenText"]);
if (seach == "Save")
{
tbl.rollNumber = mod.RollNumber;
tbl.Name = mod.StudentsName;
tbl.City = mod.City;
tbl.Address = mod.Address;
obj.tbl_StudentRegistration.AddObject(tbl);
obj.SaveChanges();
var data = from n in obj.tbl_StudentRegistration
select n;
ViewBag.Collection = data;

}
else if (seach == "Search")
{

var rno=mod.RollNumber;
var data1 = from n in obj.tbl_StudentRegistration
where n.rollNumber==rno
select n;

foreach (var d in data1)
{
model1.RollNumber = d.rollNumber;
model1.StudentsName = d.Name;
model1.City = d.City;
model1.Address = d.Address;

}

}

return View("Registration", model1);
}
My model is :

public class StudentsModel
    {
        public int RollNumber { get; set; }
        public string StudentsName { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
}



我的观点是: -

@using(Html.BeginForm()){

@ Html.ValidationSummary(true)





StudentsModel




My View is:-
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)


StudentsModel



@ Html.LabelFor(a => a.RollNumber)


@Html.LabelFor(a => a.RollNumber)






@ Html.EditorFor(a => a.RollNumber,(@ Model.RollNumber))

@Html。 ValidationMessageFor(a => a.RollNumber)


@Html.EditorFor(a => a.RollNumber,(@Model.RollNumber))
@Html.ValidationMessageFor(a => a.RollNumber)



< input type =submitvalue =搜索id =搜索/> ;


<input type="submit" value="Search" id="Search"/>



@ Html.LabelFor(a => a.StudentsName)


@Html.LabelFor(a => a.StudentsName)






@ Html.EditorFor(a => a.StudentsName,(@ Model.StudentsName))

@ Html.ValidationMessageFor(a => a.StudentsName)


@Html.EditorFor(a => a.StudentsName, (@Model.StudentsName))
@Html.ValidationMessageFor(a => a.StudentsName)

< br $> b $ b








@ Html.LabelFor(a => a.City)


@Html.LabelFor(a => a.City)






@ Html.EditorFor(a => ; a.City,@ Model.City)

@ Html.ValidationMessageFor(a => a.City)


@Html.EditorFor(a => a.City,@Model.City)
@Html.ValidationMessageFor(a => a.City)









@ Html.LabelFor(a => a.Address)


@Html.LabelFor(a => a.Address)






@ Html.EditorFor(a => a.Address,@ Model.Address)

@ Html.ValidationMessageFor(a => a.Address)


@Html.EditorFor(a => a.Address,@Model.Address)
@Html.ValidationMessageFor(a => a.Address)



< input type =submitvalue =Saveid =SAVE/>




<input type="submit" value="Save" id="SAVE" />



< input type =hiddenname =myHiddenTextid =myHiddenText/>


<input type="hidden" name="myHiddenText" id="myHiddenText" />






i希望在MVC3中显示搜索模型数据
感谢提前

i want to display search model data on view in MVC3
Thanks for advance

推荐答案

你可以这样做



you can do like this

foreach (var item in Model.StudentList)
{

 @Html.EditorFor(model=> item.RollNumber)
 @Html.EditorFor(model=> item.StudentsName )
 @Html.EditorFor(model=> item.City )
 @Html.EditorFor(model=> item.Address )

}


你必须强制输入模型才能从模型中获取值。

例如

@model ApplicationName.models.StudentsModel
You have to make model strongly typed in order to get values from model.
e.g.
@model ApplicationName.models.StudentsModel


这篇关于如何在视图元素中获取模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆