Passing a string (besides the model) into a partial view in MVC4 - asp.net-mvc-4

How can I send a string to a partial view?
What I would like is to send information about the model being viewed, to a partial view. Something like this:
#{Html.RenderPartial("_PhaseCreate", new Phase(), #Model.Id );}
Is this possible?

If you want to send some data that isn't in model or view, you should use something like the following:
1) instead of #Html.Partial(), use a #Html.Action("ActionName", "Controller", routeValues: new { id = Model.Id }) helper.
2) Add something like this to your controller:
public ActionResult GetMyView(int id)
{
ViewBag.Phase = new Phase();
ViewBag.Id = id;
// also whatever which doesn't in model ...
return View("_PhaseCreate");
}
And in your partial view, you can use those info just like you declare them:
<label>#ViewBag.Id</label>
You also can simply use the following if you just need to add data existing in model and the view:
#Html.Partial("_PhaseCreate",
new ViewDataDictionary(new { Phase = new Phase(), Id = Model.Id }))
and use them like this:
<label>#ViewData["Id"].ToString()</label>

Related

how I can implement default product in each category in mvc4 using routing

how I can implement default product in each category in mvc4 using routing?
You'll want to set up your routes like this:
routes.MapRoute("Category Route",
"categories/{category}/{product}",
new {
Controller = "Categories",
Action = "ShowCategory",
Category = "Books",
Product = ""
});
Then, your controller action method could look like this:
public CategoriesController : Controller {
public ActionResult ShowCategory(String category, String product) {
// rest of code goes here
}
}
Then inside your action you can design your repository to check String.IsNullOrEmpty(product) and determine the default one based on a database flag or however you want to indicate that.

Add all query strings on controller action

I have a simple mvc4 application. An action link opens a view with bunch of query string parameters. The view contains a simple form when you click on submit button it posts the form and comes back to the view but I lost the query strings. what I must to do to have same query strings even after you have submitted the form?
One possibility is to specify the current request as action attribute of your form:
<form action="#Request.Url.AbsoluteUri" method="post">
...
</form>
But this will POST to the same resource. If you want to specify a different controller and/or action you could write a custom BeginForm helper which will do the job.
Something along the lines of:
public static class FormExtensions
{
public static IDisposable MyBeginForm(this HtmlHelper html, string controller, string action)
{
var builder = new TagBuilder("form");
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
var routeValues = new RouteValueDictionary();
var query = html.ViewContext.RequestContext.HttpContext.Request.QueryString;
foreach (string key in query)
{
routeValues[key] = query[key];
}
builder.MergeAttribute("action", urlHelper.Action(action, controller, routeValues));
builder.MergeAttribute("method", "POST", true);
html.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
var form = new MvcForm(html.ViewContext);
return form;
}
}
and then:
#using (Html.MyBeginForm("myaction", "mycontroller"))
{
...
}
This will effectively keep the current url query string parameters.

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

*CORRECTION
The problem occurs when my view is called to populate a list from my user table.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Mike.Models.User]', but this dictionary requires a model item of type 'Mike.Models.User'.
Here is my controller action:
public ActionResult Registration(Mike.Models.User user)
{
if (ModelState.IsValid)
{
using (var db = new UserContext())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrypPass = crypto.Compute(user.password);
var sysUser = db.Users.Create();
sysUser.LastName = user.LastName;
sysUser.FirstName = user.FirstName;
sysUser.Email = user.Email;
sysUser.password = encrypPass;
sysUser.passwordSalt = crypto.Salt;
sysUser.UserID = user.UserID;
db.Users.Add(sysUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
return View(user);
}
Can someone please help me.... There are responses to similar questions on the internet but I believe mine is unique.. I have searched for weeks to no avail.
Thanks in advance,
Renior
Here is my simple controller action...
public ActionResult Index()
{
return View(db.Users.ToList());
}
and my razor syntax.
#model IEnumerable
Im trying to populate a view of my user table list..
In your Registration view at the top where your model declaration is, instead of this:
#model List<Mike.Models.User>
you need to have:
#model Mike.Models.User
You probably used strongly typed scaffolding feature to generate your view but instead of details option you chose a list option...
Take this at face value - yours is not unique. Your problem is you are passing an array of user to a controller action that expects a user.
You need to post your HTML but it is probably something like #model List user or something instead of a single user.
If your model represents a single user then pass that to the controller. If opposite, do opposite,
If you want to pass a list to the controller use list users
edit
make your razor syntax
#model Mike.Models.User

Creating a new Content Item in the migration with Orchard CMS

Here's my migration code:
public Migrations(IRepository<ProductPartRecord> productPartRepository, IRepository<CategoryPartRecord> categoryPartRepository)
{
_productPartRepository = productPartRepository;
_categoryPartRepository = categoryPartRepository;
}
public int Create() {
ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
.WithPart("CommonPart")
.WithPart("TitlePart")
.WithPart("AutoroutePart"));
ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
.WithPart("AutoroutePart", partBuilder => partBuilder
.WithSetting("AutorouteSettings.AllowCustomPattern", "true")
.WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
.WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Category Title', Pattern: 'category/{Content.Slug}', Description: 'category/category-title'}]")));
SchemaBuilder.CreateTable("CategoryPartRecord", table => table
.ContentPartRecord()
.Column<string>("Name")
.Column<string>("Description")
.Column<string>("Image")
);
ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
.WithPart("CategoryPart"));
ContentDefinitionManager.AlterTypeDefinition("Category", builder => builder
.Creatable()
.Draftable());
return 1;
}
public int UpdateFrom1() {
_categoryPartRepository.Create(new CategoryPartRecord { Name = "Category1", Description = "Description1", Image = "Image1" });
return 2;
}
UpdateFrom1 obviously attempts to insert a dummy record, but this causes nHibernate to throw this exception:
"attempted to assign id from null one-to-one property: ContentItemRecord"
The Part Record looks like:
public class CategoryPartRecord : ContentPartRecord {
public CategoryPartRecord()
{
CategoryProducts = new List<CategoryProductRecord>();
}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Image { get; set; }
public virtual IList<CategoryProductRecord> CategoryProducts { get; set; }
}
Any clues as to where I'm going wrong here? Google produced nothing.
Okay, so you are creating a contentpartrecord, not a content item there. What you want is something more along the lines of:
var item = _orchardServices.ContentManager.New("Category").As<CategoryPart>();
item.Name = "Bobs Item"; // Something like that...
item.ContentItem.As<TitlePart>().Title = "Yay a title"; // This syntax may be wrong, I'm very tired
_orchardServices.ContentManager.Create(item);
_orchardServices.ContentManager.Publish(item.ContentItem);
I think that is how you would do it. Maybe you would want to look into creating content items using the import/export module, that is the more common and safe way to do it.
Not sure if the answer from Hazza works. Haven't tried that.
I usually just do this: (But not sure if it's an inferior approach in some way)
var item = _orchardServices.ContentManager.New("Category");
var cpart = item.As<CategoryPart>();
var tpart = item.As<TitlePart>();
cpart.Name = "SomeName";
tpart.Title = "SomeTitle";
_orchardServices.ContentManager.Create(item);
But to address the comment by Lawrence Johnson:
Category in this case is the content item. He is creating a new Category content item, and then extracting the corresponding CategoryPart from it.
If you are getting null when trying to extract the part you're probably missing something.
In order for this to work you need to implement the CategoryPart, CategoryPartRecord, CategoryPartHandler and CategoryPartDriver. (And of course make sure to attach your CategoryPart to you Category content item. Not certain if placement.info is required, but would add it for consistency anyway.)
You can't leave any of these out if you plan to use a Part attached to a content item.
I'm not sure if/how you can create a Part with no content item, but you can create a Record with no part and no content item (Just make sure you don't inherit ContentPartRecord in your record object). If you simply want to add a record with no part or content item, then the code in UpdateFrom1 used by Ben Power would work for creating a record. (But migration part would have to be changed, taking out the content item and part, and manually setting the Id to be a primary key for the record)

writing route in mvc4 to eliminate query string

I have a situation where I am redirecting to an action that accepts 3 parameters. This I am doing like -
RedirectToAction("ProductSpecific", routeValues: new { partId = m.partId, categoryId= m.categoryId, categoryName = m.categoryName});
However, when the page loads, it contains all these parameters as query string.
Parts/ProductSpecific?partId=38&categoryId=1&categoryName=Monitor
I tried writing a route, but that didn't work. Can someone please guide on how to write a route in this scenario?
Thanks
The second argument of RedirectToAction is routeValues, so these will be appended to the querystring. Creating an extra route will still require you passing the values in the querystring, but like this: parts/productspecific/{partId}/{categoryId}/{categoryname} which i dont think you want.
If you dont want the values in the querystring, have a look at the TempData object, which is similar to session but will live until the next request.
Something like this:
public ActionResult DoSomething()
{
TempData["partId"] = partId;
TempData["catId"] = catId;
TempData["catName"] = catName;
return RedirectToAction("ProductSpecific");
}
public ActionResult ProductSpecific()
{
var partId = TempData["partId"];
var catId = TempData["catId"];
var catName = TempData["catName"];
var model = service.LoadProduct(partId, catId, catName);
return View(model);
}
Update:
For a route:
routes.MapRoute(
name: "ProductRoute",
url: "{controller}/{action}/{partId}/{categoryId}/{categoryname}",
defults: new { controller = "product", action = "productspecific"}
);
Add that route in the route.config class in app_start before your default routesm, and change your product specific method signature to accept the partid, catid and category name parameters. You can also use this from phil hack to profile your routes: Route Debugger