Reputation: 61
This is View ViewPage.cshtml
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
<div>Name:<a>@ViewBag.st</a><br /></div>
<input type="submit" value="Submit" />
<br />
}
My jquery for editing @ViewBag.st
<script type="text/javascript">
$(document).ready(function () {
$('a').click(function () {
var textbox = $('<input id="Text1" type="text" size="100" />')
var oldText = $(this).text();
$(this).replaceWith(textbox);
textbox.blur(function () {
var newValue = $(this).val();
$(this).replaceWith($('<a>' + newValue + '</a>'));
});
textbox.val(oldText);
});
});
</script>
My controller methos is
public ActionResult Viewdetails()
{
User ur = new User();
ur.Name = "Danny";
ViewBag.st = ur.Name;
return View();
}
And my Model Class is User.cs
public class User
{
//public string name { get; set; }
private string m_name = string.Empty;
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
}
After editing @ViewBag.title i want to store that value and pass to next View Can anyone suggest some ideas
Upvotes: 0
Views: 498
Reputation: 1038720
I would recommend you to not use ViewBag
at all.
So in your javascript you could give your textbox the same name as your User
model property (Name
):
var textbox = $('<input type="text" size="100" name="Name" />');
and then have the 2 controller actions (GET and POST):
public ActionResult Viewdetails()
{
User ur = new User();
ur.Name = "Danny";
return View(ur);
}
[HttpPost]
public ActionResult Display(User model)
{
return View(model);
}
And inside Viewdetails.cshtml
:
@model User
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
<div>Name: <a>@Model.Name</a><br /></div>
<input type="submit" value="Submit" />
<br />
}
And inside the Display.cshtml
:
@model User
<div>You have selected: @Model.Name</div>
Upvotes: 2