i have 3 model:
1st one:
public class CreateFieldModel
{
public FieldModel fm { get; set; }
public CategoryModel cm { get; set; }
}
2nd one:
public class FieldModel
{
public string field_Name { get; set; }
public InputTypeModel itm { get; set; }
public string input1 { get; set; }
public string input2 { get; set; }
public string input3 { get; set; }
public string input4 { get; set; }
public List<InputTypeModel> inputs { get; set; }
}
3rd One:
public class InputTypeModel
{
public string inputTypeName { get; set; }
public string inputTypeDesc { get; set; }
}
2 methods:
1st One:
public List<InputTypeModel> getInputTypes()
{
var inptypes = edu.InputTypes;
List<InputTypeModel> listInputTypes = new List<InputTypeModel>();
foreach (var inpType in inptypes)
{
listInputTypes.Add(new InputTypeModel { inputTypeName = inpType.Input_Type_Name, inputTypeDesc = inpType.Input_Type_Description });
}
return listInputTypes;
}
when this method executes listInputTypes has three different values.. i check it by debugging.. so no roblem here. This methos is under the class FormManagement.. I am calling this method from the following action method:
[HttpGet]
public ActionResult createNewField(CreateFieldModel cfm, string fcode)
{
FormManagement ffm = new FormManagement();
cfm.fm.inputs = ffm.getInputTypes();
return View(cfm);
}
when cfm.fm.inputs = ffm.getInputTypes(); executes it is showing "Object reference not set to an instance of an object." message... I am quite beginner to mvc.. please help
Without knowing what you really want to achieve with cfm-parameter in your action, the only thing I can suggest is to check for null references and create new instances before you assign them:
[HttpGet]
public ActionResult createNewField(CreateFieldModel cfm, string fcode)
{
FormManagement ffm = new FormManagement();
if (cfm == null)
{
cfm = new CreateFieldModel();
}
if (cfm.fm == null)
{
cfm.fm = new FieldModel();
}
cfm.fm.inputs = ffm.getInputTypes();
return View(cfm);
}
Of course, this supposes that your not relying on incoming data through your route parameters. If you are, you need to check why the values are not getting passed in, but I'm guessing you don't need it as a parameter in the first place.
Related
I need to create a generic way to add missing languages entries to all entities in which implements an specific interface. I found out how to get my collection property, but I still don't know how to add new values on it before proceed to save.
Following a piece of my public override int SaveChanges() handling.
foreach (var translationEntity in ChangeTracker.Entries(<ITranslation>))
{
if (translationEntity.State == EntityState.Added)
{
var translationEntries = translationEntity.Entity.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite &&
x.GetGetMethod().IsVirtual &&
x.PropertyType.IsGenericType == true &&
typeof(IEnumerable<ILanguage>).IsAssignableFrom(x.PropertyType) == true);
foreach (var translationEntry in translationEntries)
{
//Add missing items.
}
}
}
Classes code samples
public partial class FileType : ITranslation
{
public long FileTypeId { get; set; }
public string AcceptType { get; set; }
public virtual ICollection<FileTypeTranslation> FileTypeTranslations { get; set; }
public FileType()
{
this.FileTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class FileTypeTranslation : EntityTranslation<long, FileType>, ILanguage
{
[Required]
public string TypeName { get; set; }
}
public partial class ElementType : ITranslation
{
public long ElementTypeId { get; set; }
public string Code { get; set; }
public virtual ICollection<ElementTypeTranslation> ElementTypeTranslations { get; set; }
public ElementType()
{
this.ElementTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class ElementTypeTranslation : EntityTranslation<long, ElementType>, ILanguage
{
[Required]
public string Description { get; set; }
}
Entries from ChangeTracker have property called Entity which holds original entity
foreach (var fileType in ChangeTracker.Entries(<FileType>))
{
fileType.Entity.FileTypeTranslations.Add();
}
and for ElementType:
foreach (var elementType in ChangeTracker.Entries(<ElementType>))
{
elementType.Entity.ElementTypeTranslations.Add();
}
I didn't test, but it was too long to paste in comment.
i have an entity clss for stuffs:
public partial class Stuffs
{
public Stuffs()
{
this.Stuffs1 = new HashSet<Stuffs>();
}
public long StuffID { get; set; }
public Nullable<long> StuffParentID { get; set; }
public string StuffTitle { get; set; }
public virtual ICollection<Stuffs> Stuffs1 { get; set; }
public virtual Stuffs Stuffs2 { get; set; }
}
when i send List to stimulereport , i want to show result in master-detail or parent-child.
these are my stimule code:
public ActionResult PrintTreeStuffsResult()
{
List<Stuffs> StuffListResult = db.Stuffs.ToList();
StiReport StiRpt = new StiReport();
StiRpt = GetStiReportTree(StuffListResult);
return StiMvcViewerFx.GetReportSnapshotResult(StiRpt);
}
public StiReport GetStiReportTree(List<Stuffs> StuffsTreeResult)
{
StiReport StiRpt = new StiReport();
string a = HttpContext.Server.MapPath("~/Content/Reports/Admin/StuffsTreeResult.mrt");
StiRpt.Load(a);
StiRpt.RegBusinessObject("Stuffs", StuffsTreeResult);
StiRpt.Dictionary.SynchronizeBusinessObjects();
StiRpt.Save(HttpContext.Server.MapPath("~/Content/Reports/Admin/StuffsTreeResult.mrt"));
return StiRpt;
}
i saw in internet many samples that explains about two table with relation , forexample customer and order. but i have one table with self-joined.
how can i do that?
Thanks...
Try to use next method:
StiRpt.Dictionary.SynchronizeBusinessObjects(2);
Is there a way in MVC to pass information from one controller to another? I have a character model that looks like this:
public class Character
{
[Key]
public string CharacterID { get; set; }
public string UserID { get; set; }
public string Name { get; set; }
public int Str { get; set; }
public int Con { get; set; }
public int Dex { get; set; }
public int Int { get; set; }
public int Wis { get; set; }
public int Cha { get; set; }
public int BaseAttack { get; set; }
}
And a separate weapon model like this:
public class Weapons
{
[Key]
public string WeaponID { get; set; }
public string UserID { get; set; }
public string CharacterID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Range { get; set; }
public int Damage { get; set; }
public int Crit { get; set; }
public int CritMultiplier { get; set; }
public string Hands { get; set; }
public string Distance { get; set; }
}
To create a weapon, you first need to create a character which assigned an ID, and I want to be able to pass that ID into the create method of my weapon controller. Is there a way to do this? Thanks
You can use TempData for this purpose. TempData stores data only between two requests. When you set the TempData the next request that is initiated can retrieve value from the TempData and it will be erased for any consequent requests.
[HttPost]
public ActionResult CreateCharacter()
{
// creates charaeters here and sets the tempdata
TempData['CharacterId'] = 50;
return RedirectToAction('CreateWeapon');
}
[HttpGet]
public ActionResult CreateWeapon()
{
var weaponModel = new WeaponModel() { CharacterId = (int)TempData['CharacterId'] };
return View(weaponModel);
}
and in your view simply have a hidden for the CharacterId, so it will be persisted if you your post fails validation or if you need to re-display the view.
#Html.HiddenFor(e => e.CharacterId);
Again this is just one approach, only if you you don't want to pass the CharacterId in the url.
You can also achive this just by passing it in the url:
[HttPost]
public ActionResult CreateCharacter()
{
// creates charaeters here and sets the tempdata
return RedirectToAction('CreateWeapon', new { characterId = 50 });
}
[HttpGet]
public ActionResult CreateWeapon(int characterId)
{
var weaponModel = new WeaponModel() { CharacterId = characterId };
return View(weaponModel);
}
I would be inclined to pass the character id to the create weapon action via routing, either as a route token that forms part of the path or via the query string. Be sure to check that the weapon can logically be associated with the character to whom the id corresponds.
You could also pass the id using TempData or Session, but considering both by default will take up memory on the web server, the simple option is to use the routing. In addition, unless you call TempData.Keep("key") after accessing TempData, the value will be removed from TempData after the first access, potentially causing issues if the user refreshes the browser window.
You could use RedirectToAction(), though as titled this will cause browser redirection.
return RedirectToAction("CreateWeapon", "Weapon", new { id = yourid });
or
#Html.ActionLink("CreateWeapon", "Create", new { id = yourid })
Edit: Your plain object property names and your action method variables need to match, to do this.
I want get some qualification about reloading model in mvc action. For example:
I have some class model:
public class PresentationItemModel()
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public List<int> PresentationIdList { get; set; }
}
And some controller action:
public ActionResult PostAction(PresentationItemModel model)
{
...
if(model.PresentationIdList == null)
{
model.PresentationIdList = new List<int>();
}
model.PresentationIdList.Add(model.Id);
...
...
...
}
I can call PostAction method several times and I want to save model.PresentationIdList result with all id's. But every time my PresentationIdList reloading with all model. But it's standard behavior.
Can I resolve it?
All you need to do is return the model object from your PostAction:
public ActionResult PostAction(PresentationItemModel model)
{
...
if(model.PresentationIdList == null)
{
model.PresentationIdList = new List<int>();
}
model.PresentationIdList.Add(model.Id);
...
...
...
return new ActionResult(model);
}
I have a form I am trying to send in a GetJSON call. When I get to the Controller the model that si tied to the view is a null vlaue. I have had issues before dealign with returning data when I woudl get an empy object but never a null value. Below is the code I am using to send the form
var cqvdata = $("form").serialize();
$.getJSON('#Url.Action("GetEmailByAdvanced", "CustomerEmails")', { cqv: cqvdata }, function (contacts) {
var emails = "";
$.each(contacts, function (index, contact) {
$('#BCCText').tagit('createTag', contact.Email)
});
return false;
});
Below is what I have on the controller side
public JsonResult GetEmailByAdvanced(MassEmailViewModel cqv)
{
}
Here is what I get for results if I turn my argument into a string
"EmailFromAddressID=1&ToAddresses=&CCAddresses=bclairmont%40harr.com&BCCAddresses=adunn%40harr.com&Subject=&Body="
Below is the MassEmailViewModelClass and all sub classes
public class MassEmailViewModel
{
public MassEmailViewModel()
{
ComplexQuery = new CustomerQueryViewModel();
}
public int EmailFromAddressID { get; set; }
public CustomerQueryViewModel ComplexQuery { get; set; }
public string ToAddresses { get; set; }
public string CCAddresses { get; set; }
public string BCCAddresses { get; set; }
public string Subject { get; set; }
[AllowHtml]
public string Body { get; set; }
}
public class CustomerQueryViewModel
{
public CustomerQueryViewModel()
{
Products = new List<CustomerProductQueryProduct>();
Details = new List<CustomerQueryDetail>();
}
public Boolean IncludeOnAll { get; set; }
public Boolean ExcludeOnAll { get; set; }
public List<CustomerProductQueryProduct> Products { get; set; }
public List<CustomerQueryDetail> Details { get; set; }
}
public class CustomerProductQueryProduct
{
public CustomerProductQueryProduct()
{
ProductDetails = new List<CustomerProductQueryProductDetail>();
ProductVersions = new List<ProductVersion>();
}
public ProductType ProductType { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public int VersiondID { get; set; }
public List<CustomerProductQueryProductDetail> ProductDetails { get; set; }
public List<ProductVersion> ProductVersions { get; set; }
}
public class CustomerProductQueryProductDetail
{
public ProductTypeDetail ProductDetail { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public string Value { get; set; }
public string Value2 { get; set; }
}
public class CustomerQueryDetail
{
public string Description { get; set; }
public string Type { get; set; }
public Boolean Exclude { get; set; }
public Boolean Include { get; set; }
public string Value { get; set; }
public string Value2 { get; set; }
}
The only thing not being returned is my ComplexQuery in the serialize because I am using a JQuery dialog so it takes those elements out of the form. I woudl think I woudl get a MassEmaikViewModel with all the vlaues but ComplexQuery and have a null for that but I just get a null as iff the argument never even got initialized.
Any ideas on what could be causing this?
One other thing and I don't know if this will help give anyone any insight or not but I can post from the form and have the MassEmailViewModel as the argument in the post and it works fine filling out all the values except for ComplexQuery
I figured it out after a ton of trial and error. It seems like GetJSON can't handle passing the data. What I did to correctly get information was to change to an AJAX get call. I will post the code below
$.ajax({
url: '#Url.Action("GetEmailByAdvanced", "CustomerEmails")',
type: 'GET',
data: cqvdata,
success: function (data) {
//called when successful
var emails = "";
$.each(contacts, function (index, contact) {
$('#BCCText').tagit('createTag', contact.Email)
});
return false;
},
error: function (e) {
//called when there is an error
//console.log(e.message);
}
});
I used the exact data I had in the GetJSON. In fact I commented out the GetJSON and just put this in below it and I got my model filled in on the controller side.