MVC Passing Model Together With Files Back To Controller?
Oct 6, 2010
I've been going at this for several hours and I simply cannot find the solution.
I want to get some data from my user. So first, I use a controller to create a view which receives a Model:
[code]...
The rest of this controller does not matter since no matter what I do, the count attribute of Request.Files (or Request.Files.Keys) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).
I have following situation - I am pasing user info object from Controller to View. It contains GUID UserID, which i dont want to be seen on page. So I removed every Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc... from generated View source. And because of this when Html.BeginForm() returns that object back to Controller all values is there but UserID is lost??
If I leave Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc.. in View everything is fine. But I dont want to show UserID? Where is the problem here?
Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back. In my example, I have a Class called "Ingredient" which has 4 public accessories: Name, Barcode, Amount, and Unit. [Code]....
Or is this not possible? (Basically, I'm trying to re-create a datagrid where certain fields are editable and certain are not...)
I have a simple model where a Person has Gifts. I have a view which is a list of Gifts belonging to one Person.
My problem is with the Create action for a new Gift. I want it to default to the PersonID that we are already viewing the list of Gifts for. I tried simply passing the last PersonID (they are all the same)
Html.ActionLink("Create New", "Create", new { id = Model.Last().PersonID }) which works fine if there is already at least one Gift for that person but if this is the first Gift I don't have a value.
My Gift List controller knows the PersonID I want to pass but the view doesn't.
How do I pass this PersonID from my Gift List controller to my Gift Create controller via the Gift List view? Or is there a better way to do this?
Being new to ASP.NET I have run into trouble building my own Whack-a-mole program. I think my problem comes from using Buttons, which by themselves send post backs to the server, making the software unusable. The looks are in place, making new buttons show up in the grid, in different places by random. However, when a button is pushed - the score doesn't change (which I feel is strange).
Not so strange is that the Button doesn't work since it sends post back to the server - reloading the UpdatePanel. I think I should use a different controller like the CheckBox and style it hard using CSS (which isn't a problem). Is this the correct way to go, or should I make use of JavaScript AJAX instead?
Note to self: This technique shouldn't be used in a public application since it put too much unwanted pressure on the web server.
i need to implement edit functionality for my model. among other fields my model contains record id in the database. when my controller is called i need to update the database record with the same id as the model using the fields in the model.
here is my controller:
[HttpPost] public ActionResult Edit(MyModel m) { var r = my_datacontext.MyTable.SelectSingleOrDefault(x => x.id == m.id); if (r != null) { r = m; my_datacontext.SubmitChanges(); } }
this does not work because m does not get added to the datacontext's ChangeSet. why? i fixed this by changing the signature of my controller action to receive FormCollection instead of the model, and by invoking UpdateModel manually. i think this is not the right way to do it.
In MVCs with which I have worked, the Controller has the job of co-ordinating a number of views, consequent to some user action against the model.
However in ASP MVC, there never appears to be more than 1 view resulting from an http request (please correct me if I am wrong). Instead the "Controller" in ASP MVC appears to be a URL Routing Target.
Also, in my (admittedly limited) experience, the Model in MVC is intended to be a model of the problem domain of the application. However in ASP MVC, the "Model" appears to be a model of the data binding of the corresponding view.
Has anybody figured out a way to separate the dependency of the view/controller from the model. In other words, model is independent of the view/controller in asp.net MVC but has anybody also taken out dependency of the view/controller from model as well?
I am using the Nhibernate with MVC. In the controller i am getting the model from the HttpSession. How can i set the model from controller Unit testing method.
I have setup an httppost that sends a string into my controller, searches for some results using linq, and then sends the results to a view. In debug stepping through the code I can see the data that I am looking for being passed into the return view statement, but the page just appears to refresh (it doesn't render the view as expected with the result). why my controller fails to redender the view? (note: I didn't include the view because I can send a ToList() to it without an issue. For example, return View(_entities.Persons.ToList());
[HttpPost] public ActionResult RenderSearch(string usersearchtext) { if (usersearchtext != null) { var search_results = from s in _entities.Persons where s.Description.Contains(usersearchtext) select s; return View("SearchResult", search_results.ToList()); } else { throw new NotImplementedException(); } }
I would like to pass List<SelectListItem> (not selected item but the whole "List" object) back to controller.
for example in my GET controller i would have this
ViewData["list"] = some select list from repository;
then on post I would like to get the list back from view..pretty much i'm trying to use ViewData["list"] as storage..this way I would use ajax to remove or add items to/from the list.
i can access the value text box by AgencyID but if i clicked on Second record AgencyID returns first recode id totally AgencyID returns the first record id.
I am new to MVC, and trying something and got stuck somewhere in between.I have a user control there I have three textbox html type(ID, Lastname, firstname) and a submit buttom. I set the button like
I have called this usercontrol on some view through
<%= Html.Partial("ucName") %>
Now on pressing that button(on user control) I need to pass the data from these textboxes to controller again to some specific action(Http Post action). By using this data I want to do some database interaction and storing the result in a dataset and pass this data set to same view again to show up in some Grid.I know the first part in conventional Asp.net can be done by raising the event through delegate but don't know how to do that in MVC.
I have a dictionary I'm passing to a View. I want to be able to pass the values of that dictionary back to a Controller action from this same View. Is there anyway this can be accomplished without using a form? The situation is that I need to be able to pass these back to a controller action that is called when a user clicks an ActionLink from the View, and in my experience an ActionLink cannot be used to submit a values of a form, i.e. I've only been able to submit values of a form using the form's submit button. Unless there's a way to use an ActionLink to submit values in a form.Controller Action passes Dictionary to Controller in ViewData:
public ActionResult ModifyNewCrossListing(FormCollection form) { Dictionary<int, string> prefixlist = new Dictionary<int, String>();
In my controller I UpdateModel(model). That works. I'm getting the model from the view.I save the fields of the model to a business entity and save the entity through a business layer to my database. The business layer returns a unique record id which I store in my model. Then I pass the model to the View.
return View(model);
After the View loads the Model in the View has not been updated. The unique record id has not been changed. I have been at this for four hours and I'm not getting anywhere. I have been trying to convert a windows forms project to MVC2 for six weeks and it just gets more and more frustrating. None of the tutorials on the MS site or the Microsoft book I have answer any of my questions.