I think I have a strong typed View the line below is the first item in the view.
@model MvcWebApprovalClient.Models.DocumentModel
@Html.TextBox(model.titleReview, null, new { id = "titleReview", disabled = true, size = "40" })
But when I change the code to use the model I get an error
Error 1 The name 'model' does not exist in the current context f:\Users\mtod\My Documents\My Development Projects\MvcApplication3\MvcApplication3\Views\Home\Index.cshtml 118 27 MvcWebApprovalClient
mtod
0 Points
14 Posts
Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 04:52 PM|LINK
I need some help with modifying data in a controller and updating it in the view.
The goal is to click the button make a call to Jscript to execute an action method in the controller to update text in a textbox.
I get this all the way thur the controller and return to the View but the View does not update.
Note: the button call is not doing a submit only a onclick to jscript then the controller action.
Thanks for the help.
Here’s my code.
Model:
public class DocumentModel{….public string titleReview { get; set; }….}Controller: “Home”
[HttpPost]
public ActionResult DocumentApproved(string foo, string bar)
{
ViewData["titleReview"] = "Test Document";
return View("Index");
}
View: “index”
<script type="text/javascript">function DocumentApproved() {$.post("/Home/DocumentApproved", { foo: $("#foo").val(), bar: $("#bar").val() });}</script><div id="leftbody">
@Html.TextBox("titleReview", null, new { id = "titleReview", disabled = true, size = "40" })</div><div><input type="button" value="Approve" id="DocumentApprovedBt" onclick="DocumentApproved()"/></div>megh1207
Contributor
2274 Points
494 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 05:43 PM|LINK
you can use strongly typed view and set control id and set value from post back
after strongly bind you can able to use this method to set value
@Html.TextBoxFor(model.TitleReview)
mtod
0 Points
14 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 06:03 PM|LINK
I think I have a strong typed View the line below is the first item in the view.
@model MvcWebApprovalClient.Models.DocumentModel
@Html.TextBox(model.titleReview, null, new { id = "titleReview", disabled = true, size = "40" })
But when I change the code to use the model I get an error
Error 1 The name 'model' does not exist in the current context f:\Users\mtod\My Documents\My Development Projects\MvcApplication3\MvcApplication3\Views\Home\Index.cshtml 118 27 MvcWebApprovalClient
Any idea why this would happen?
Thanks for the help.
megh1207
Contributor
2274 Points
494 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 06:09 PM|LINK
so to set value that you update all you need is to return model with value that you want to set like
DocumentModel obModel = new DocumentModel();
obModel.titleReview = "your value";
return View(obModel);
resplace
@Html.TextBox(model.titleReview, null, new { id = "titleReview", disabled = true, size = "40" })
with
@Html.TextBoxfor(m=>m.titleReview, null, new { id = "titleReview", disabled = true, size = "40" })
hope this would help
mtod
0 Points
14 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 07:46 PM|LINK
I tried it as you outlined but it does not update the TextBox.
FYI this is MVC 4 not sure if that would make any difference.
Thanks
CPrakash82
All-Star
18268 Points
2838 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 17, 2012 11:17 PM|LINK
Did you got any error after trying that? You can debug the view code and see what you are getting from the Action Method in view (Model value)
Steps given above should work, MVC4 doesn't have to do with this issue.
Thanks,
mtod
0 Points
14 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 18, 2012 06:06 PM|LINK
No error messages it just goes right thru the method and back to the page but does not update the textbox.
I think I must be missing something with the Model/View interaction.
CPrakash82
All-Star
18268 Points
2838 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 18, 2012 07:30 PM|LINK
Yes, you can place a breakpoint in your view file and check the value of model in debug window.
Thanks,
mtod
0 Points
14 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 18, 2012 07:38 PM|LINK
Looks like it never refreshes the page when returns from the action method.
Thanks
mtod
0 Points
14 Posts
Re: Looking for some help with modifying data in a controller and updating it in the view.
Jul 19, 2012 01:50 AM|LINK
When I do a break point in the view the model or m is not an instance.
Where do you create the instance in the index method? I'm not sure how you like the Model to the View.
Thanks for the help.