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; }
}
My View is:-
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
StudentsModel
@Html.LabelFor(a => a.RollNumber)
@Html.EditorFor(a => a.RollNumber,(@Model.RollNumber))
@Html.ValidationMessageFor(a => a.RollNumber)
<input type="submit" value="Search" id="Search"/>
@Html.LabelFor(a => a.StudentsName)
@Html.EditorFor(a => a.StudentsName, (@Model.StudentsName))
@Html.ValidationMessageFor(a => a.StudentsName)
@Html.LabelFor(a => a.City)
@Html.EditorFor(a => a.City,@Model.City)
@Html.ValidationMessageFor(a => a.City)
@Html.LabelFor(a => a.Address)
@Html.EditorFor(a => a.Address,@Model.Address)
@Html.ValidationMessageFor(a => a.Address)
<input type="submit" value="Save" id="SAVE" />
<input type="hidden" name="myHiddenText" id="myHiddenText" />
i want to display search model data on view in MVC3
Thanks for advance