MVC 4 Partial view with different model called with AJAX - asp.net-mvc-4

I'm trying to solve a problem. I am new to MVC and trying to understand how the models work. I get the main page and main model stuff and that if you want to show a partial view the data you show must be part of the main model. This makes the main model pretty large and doesn't that require me to submit the entire model to get the partial view? In any case, let's say I have a list of things and one column is a link. This link calls an AJAX method to get more data and display in a jQuery dialog - my goal. So I would have a call like this:
function showDetails(id) {
$("#divShowDetails").load('#(Url.Action("GetDetails", "Home", null, Request.Url.Scheme))?Id= ' + id);
}
My view is like "_DetailsView.cshtml", defined as a partial view. Does this page need to define the model in the main page or can it be a different model or no model at all? Can I return a ViewData from the controller method, pop open the dialog and fill it with data?
Should the controller method return a PartialViewResult or just an ActionResult? Let's say the details view are details of a part number or something and I want to show a bunch of data elements for the part. Does this have to me a model? I am confused and any help would be greatly appreciated.
Thanks guys. Do you still use the common:
[HttpGet]
public PartialViewResult SelectedItem (string itemId)
{
// gather data for the item
return PartialView(itemModel);
}
And you are saying that the itemModel does not have to be part of the item list model?

Related

Passing data from a controller to a view (MVC)

I am having a small issue where I cannot pass the data to the view.
In the controller I have
ViewBag.InvalidParts = Invalid;
In the view I have
<h2>#Html.DisplayText("InvalidParts")</h2>
I get this error on my web page:
System.Collections.Generic.List`1[System.String]
When I step through my code I do see that my view bag has 20 records stored in it, my problems is that I cannot view them. Am I making a wrong call? Thanks in advance for the help
in your foreach you need to use the variable that you have defined. In this situation I don't think you need the helper. Try changing your code to
#foreach(var invalid in ViewBag.InvalidParts){
<text>
<h2>#invalid</h2>
</text>
}

Two views 1 Model MVC

When using MVC to capture information in a form, can I break up the from in two views but still use a single model, and write to the db at the last form?
For example Account.cs model has name and surname
I want view1 to capture name, view2 to capture surname and when i click submit it must write to the db.
You can use a model on however many views you like. In fact, one of the features of MVC is that it allows you to use the same model and controller action with different views to present the same data in difference way.
However, in this case, you will be responsible for getting the data from the first view into the second view, so the submit action can write both together.
I don't believe it is possible to use a single form across two views. However, you can use two forms for the two views, using a single model. In view1, capture the name and call the following controller action:
public ActionResult(Account model)
{
return View("View2", model");
}
Then, simply use a hidden input to store the information from view1 in the form in view2:
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
#Html.HiddenFor(x => x.Name)
#Html.TextBoxFor(x => x.Surname)
</div>
}

Yii CListView ajax post data

When using Yii CListView with Ajax, when I click next or previous button using Yii default Pager, I want to post additional parameters to controller - For example, I want to post search keyword, userid...etc to update criteria in the controller - BUT I DON'T KNOW HOW TO DO THAT.
Can anyone help?
Thanks in advanced,
I think you should to extend CListView component.
public function registerClientScript()
{
/*deleted*/
$options=array(
'ajaxUpdate'=>$ajaxUpdate,
'ajaxVar'=>$this->ajaxVar,
'pagerClass'=>$this->pagerCssClass,
'loadingClass'=>$this->loadingCssClass,
'sorterClass'=>$this->sorterCssClass,
);
/*deleted*/
$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiListView($options);");
}
May be you should append your own params to the options array.

ErrorSummary for multiform model

I am using a multi form model. A $model array is passed to the view and for each model object I am trying to have an errorsummary. See the code below.
foreach ($model as $f=>$edu):
echo $form->errorSummary($edu,'');
echo $form->textField($edu,"[$f]schoolname",array('size'=>30,'maxlength'=>128));
endforeach;
When I submit the form an error summary for only one form is displayed. Any ideas.
Maybe you need to rethink your form structure, for example - put all into one form, then inside controller make validation of every model you use and then show error messages.

ObservableCollection<someentity> not refreshing

I am using Silverlight 4 and MVVM pattern for my application. I have a listbox that is bound to one page say one.xaml and it's viewmodel is oneviewmodel.cs. This is the page where i load my albums collection. I have a button on that page which popups a page to add a new album. Say that page is two.xaml and it's viewmodel is twoViewModel.cs. On this page i call ria services :-
context.albums.add(somealbum);
and submit the changes.The album gets added and i can see the record in sql server. However when the popup gets closed my listbox still shows the stale data. Do i need to again make a request to server to load the fresh entity just added? Thus, essentially i have to use messaging pattern and request oneviewmodel.cs to load the entities again. Is this correct way of doing?
This is my method of loading album entities :-
var qry = AlbumContext.GetAlbumsQuery(_profile.UserId);
AlbumContext.Load<Album>(qry, new Action<System.ServiceModel.DomainServices.Client.LoadOperation<Album>>(albums => {
if (GetAlbumsComplete != null)
{
if (albums.Error == null)
{
GetAlbumsComplete(this, new EntityResultArgs<Album>(albums.Entities));
}
else
{
GetAlbumsComplete(this,new EntityResultArgs<Album>(albums.Error));
}
}
}), null);
This is using the same pattern and classes as Shawn Wildermuth.
Thanks in advance :)
You do not need to load everything from the server again, but you need to add the new album to your ObservableCollection. So far you only added it to the DomainContext.
You could do one of the following two options:
1) Add the new album directly to the collection with
collection.Add(somealbum);
or
2) I assume that you fill the ObservableCollection in GetAlbumsComplete(). Just execute that part again, so that the ObservableCollection is filled with the content of your DomainContext.Albums.