Hi ashwinijadhav...,
If you want to use TextBoxFor then you need to use strongly typed model in your view instead of ViewBag.
If you want just a TextBox there are two options.
Use Html.TextBox or Input type TextBox.
Refer below sample.
Model
public class applicant
{
public int id { get; set; }
public string ack_no { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Create()
{
List<applicant> lastApplicant = new List<applicant>()
{
new applicant{id=1,ack_no="1"},
new applicant{id=2,ack_no="2"},
new applicant{id=3,ack_no="5"}
};
int id = 3;
var ack_no = (from d in lastApplicant.Where(x => x.id == id)
select d).FirstOrDefault().ack_no;
ViewBag.ack_no = ack_no;
return View();
}
}
View
Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
<input type="submit" value="Create" />
}
</body>
</html>
Create
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
<div>
<h4>Using TextBox</h4>
@Html.TextBox("ack_no", (string)ViewBag.ack_no)
<h4>Using input type TextBox</h4>
<input type="text" name="ack_no" value="@ViewBag.ack_no" />
</div>
</body>
</html>
Screenshot