|
I thought I'd take an hour and try to make my project more efficient by taking HTML with Razor that is common to many pages and packaging them up. So I have block of HTML and Razor Html Helpers called Avatar, in which it shows the selected avatar, you can upload a new, and a sliding horizontal panel that you can choose one from.
I'm not even sure if I'm on the right track here. In webforms, I would of made a user or server control out of it.
So I made a Helper in App_Code. First I made it as a view in the view folder. And searched and results said no make it the App_Code folder. So I do that, then I had to change the @using. Then I wanted to pass any mode to it.
I ended up with this in the App_Code folder marked as Build Action: None.
Oh and the hour is now 2 hours.
I can't figure out how to build and use it.
Question: Am I wasting my time here on this. Should I use a different method. What do you think?
[The Razor seems to mess up my formatting]
@using System.Web.Mvc.Razor;
@functions {
private static WebViewPage page { get { return PageContext.Page as WebViewPage; } }
private static System.Web.Mvc.HtmlHelper Html { get { return page.Html; } }
private static UrlHelper Url { get { return page.Url; } }
private static dynamic ViewBag { get { return page.ViewBag; } }
}
@helper avatar_select(object model)
{
<pre>
<div class="panel panel-default">
<div class="panel-heading">
<h4>Avatars</h4>
</div>
<div class="panel-body">
<div class="col-sm-3">
<div class="form-group">
@Html.LabelFor(m => m.Avatar_Primary_ID, new { @class = "control-label" })
<div class="avatarImage">
<img id="avatarPrimary_Select" data-avatarid="@Model.Avatar_Primary_ID" src="@String.Format("data:image/png;base64,{0}", Model.Avatar_Primary_Image.Base64)" alt="@Model.Name" height="100" />
</div>
<div class="avatarDelete">
<a id="AvatarDeleteButton" class="btn btn-primary has-spinner" name="AvatarDeleteButton">
Delete
<span class="spinner"><i class="fa fa-cog fa-spin"></i></span>
</a>
</div>
<div>
@Html.HiddenFor(m => m.Avatar_Primary_ID)
@Html.HiddenFor(m => m.Avatar_Primary_Image.Base64)
@Html.HiddenFor(m => m.Avatar_Secondary_ID)
@Html.HiddenFor(m => m.Avatar_Secondary_Image.Base64)
</div>
</div>
</div>
<div class="col-sm-9">
<div class="avatarUploadMessage">
<span class="text-primary"><strong>You may upload a new avatar</strong></span>
</div>
<div class="avatarUploadControls">
<div class="form-group">
<label class="control-label">Enter your avatar's name max(20)</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-tag"></i>
</span>
<input id="AvatarName" class="form-control" type="text" maxlength="20" placeholder="Name your Avatar" value="@Model.Avatar_Primary_Image.Name">
<span class="input-group-addon">
<i class="fa fa-comment"></i>
</span>
<input id="AvatarAlt" class="form-control" type="text" maxlength="10" placeholder="Alternate Name" value="@Model.Avatar_Primary_Image.Alt">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Choose
<input id="AvatarBrowse" name="AvatarBrowse" class="btn btn-primary spinner" type="file" accept="image/*" />
<i id="AvatarSpinner" class=""></i>
</span>
</span>
</div>
</div>
</div>
<div class="avatarPreviewMessage">
<span class="text-primary"><strong>Or select one from your collection</strong></span>
</div>
<div id="avatarPreviewContainer">
<div id="avatarPreviewSlider" name="avatarPreviewSlider">
<img src="~/Content/themes/base/images/indicator_Big.gif" style="padding-top: 6px;" />
</div>
</div>
<div id="log" class="text-info" />
<div class="avatarPreviewControlsHelp">
<span id="avatarInstructions" class="text-primary">Touch and swipe to scroll more</span>
</div>
</div>
</div>
}
|
|
|
|
|
basically i am showing checkboxes in page and checkboxes generated in loop. i have create a custom validation using this
ValidationAttribute, IClientValidatable
my server side validation working fine but client side is not firing. please have a look at my code and tell me where i made the mistake.
My Goal
i want user has to select one hobby means user has to select one check boxes out of many.
this way i am generating checkboxes
<div class="col-md-offset-2 col-md-10">
Hobbies<br />
@for (int x = 0; x < Model.Hobbies.Count(); x++)
{
@Html.CheckBoxFor(p => p.Hobbies[x].IsSelected) @:
@Html.LabelFor(p => p.Hobbies[x].IsSelected, Model.Hobbies[x].Name) @:
@Html.HiddenFor(p => p.Hobbies[x].Name)
}
@Html.ValidationMessageFor(model => model.Hobbies)
</div>
this way i annotate my view model property
[AtleastOne(ErrorMessage = "Select at least one checkbox.")]
public List<Hobby> Hobbies { get; set; }
here is code for custom validation.
public class AtleastOneAttribute : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var oHobby=value as IEnumerable;
foreach (var _object in oHobby)
{
Hobby _oHobby = (Hobby)_object;
if (_oHobby.IsSelected)
{
return ValidationResult.Success;
}
}
}
return new ValidationResult(ErrorMessage);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "atleastonetrue",
};
yield return rule;
}
}
client side code
<script type="text/javascript">
$.validator.unobtrusive.adapters.add('atleastonetrue', function (options) {
options.messages['atleastonetrue'] = options.message;
alert('hello');
alert(options.message);
});
$.validator.addMethod("atleastonetrue", function (value, element, param) {
alert('hello');
});
</script>
where i made the mistake in my code for which client side code is not firing?
Generated html for checkbox
<div class="col-md-offset-2 col-md-10">
Hobbies<br>
<input type="checkbox" value="true" name="Hobbies[0].IsSelected" id="Hobbies_0__IsSelected" data-val-required="The IsSelected field is required." data-val="true"><input type="hidden" value="false" name="Hobbies[0].IsSelected">
<label for="Hobbies_0__IsSelected">Reading</label>
<input type="hidden" value="Reading" name="Hobbies[0].Name" id="Hobbies_0__Name"><span data-valmsg-replace="true" data-valmsg-for="Hobbies[0].IsSelected" class="field-validation-valid"></span><input type="checkbox" value="true" name="Hobbies[1].IsSelected" id="Hobbies_1__IsSelected" data-val-required="The IsSelected field is required." data-val="true"><input type="hidden" value="false" name="Hobbies[1].IsSelected">
<label for="Hobbies_1__IsSelected">Sports</label>
<input type="hidden" value="Sports" name="Hobbies[1].Name" id="Hobbies_1__Name"><span data-valmsg-replace="true" data-valmsg-for="Hobbies[1].IsSelected" class="field-validation-valid"></span><input type="checkbox" value="true" name="Hobbies[2].IsSelected" id="Hobbies_2__IsSelected" data-val-required="The IsSelected field is required." data-val="true"><input type="hidden" value="false" name="Hobbies[2].IsSelected">
<label for="Hobbies_2__IsSelected">Movies</label>
<input type="hidden" value="Movies" name="Hobbies[2].Name" id="Hobbies_2__Name"><span data-valmsg-replace="true" data-valmsg-for="Hobbies[2].IsSelected" class="field-validation-valid"></span>
</div>
My Model and ViewModel
public class Product
{
public int ID { set; get; }
public string Name { set; get; }
}
public class Hobby
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
public class SampleViewModel
{
[Display(Name = "Products")]
public List<Product> Products { set; get; }
[AtleastOne(ErrorMessage = "Select at least one checkbox.")]
public List<Hobby> Hobbies { get; set; }
[Required(ErrorMessage = "Select any Product")]
public int SelectedProductId { set; get; }
[Required(ErrorMessage = "Select Male or Female")]
public string Gender { get; set; }
}
thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: i want user has to select one hobby means user has to select one check boxes out of many.
Then you're using the wrong control. If the user can only select a single option, use a set of radio buttons[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi all.IN many example i download here they use javascript.
If I creat a page without masterpage.I know where to put this Javascript.But if I use Masterpage.WHere to put these script.
Thanks
|
|
|
|
|
Do you mean js on the page? Or in a separate file? If it's in a separate file then put it in a "scripts" folder and refer to it like this
<script src="<%=Page.ResolveUrl("~/scripts/file.js") %>"></script>
If you're referring to js put on the page, then if the js uses elements on the master page put it on the master page, if it references elements on the content page put it on the content page, if it references both then put it on the content page also.
|
|
|
|
|
I Mean JS on the page, not in a separate file.
The js is only use in Content Page.I Know I have to put it on the content page.But I don't know where to put it.
I am a beginner.
|
|
|
|
|
|
I'm using a webgrid in a MVC5 app. I'm having two issues:
- The last row does not have the same height as the previous rows. It's off by a couple of pixels. I'm not setting the height of the grid, it's parent div or anything like that, so I don't know why this is happening.
- I want three of the columns to disappear when the browser resolution gets below a certain size (when the device is a phone used in portrait mode). When the resolution changes to trigger the style change, the data columns are hidden, but the headers are still there.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 24-Mar-16 9:41am.
|
|
|
|
|
I've also tried MvcContrib.Grid, and a different issue with that.
I'm trying to put two action links in the last cell of each row. They are floating divs, but they refuse to line up.
The deletable columns also still don't delete.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
EVERYTHING SOLVED.
Conclusion - WebGrid is a half-ass control. MvcContribGrid is difficult to adjust to if you started out messing with WebGrid. There are some issues with Contrib.Grid regarding the style of the header row cells. When I use the Header(string), I get a warning saying the overload is obsolete, but I have to use that to get the borders to display correctly.
I got the deletable columns thing working (I had forgotten to uncomment the style spec for the appropriate style inside the media check).
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Web development sucks, doesn't it?
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Why, yes, yes it does.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Especially if one has to maintain something like this monstrosity[^].
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Alright, I have my login stuff sorted, and it's time to start adding models that actually serve the web site's purpose. I'm a complete EF virgin, having been able to successfully avoid using it until today. What is the best (or typical) approach to adding models? Should each model (or model set) get its own dbcontext?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
God, no.
There are a few schools of though as to the appropriate use of DbContexts. The two that make the most sense to me are that you separate the models into domains and use a context per model domain, and the other is that you have a DbContext for each separate database. I prefer the latter, unless your object count bloats obscenely.
The important part is that the DbContext implements both the repository and UoW patterns, so design it as you would any repository and use it as a UoW.
The primary guidance that the Ef design team has always espoused is that only the first instance of a particular DbContext has much cost, after that the model has been generated and any further instances are cheap.
I hope that helps.
|
|
|
|
|
I only have one database. Would it make sense to use the AppDbContext (derived from IdentityDbContext) that already exists?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
No, I would quantify that as an object domain issue; Authentication and Authorization information should be separate from operational models.
|
|
|
|
|
Yep, I would leave the IdentityDbContext alone as it is used internally for Identity Framework. You should use your own DbContext for your database. I would recommend one dbcontext for one db.
|
|
|
|
|
Nathan Minier wrote: The important part is that the DbContext implements both the repository and UoW patterns,
Isn't it the DbSet that acts as the repository?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's a fair question, and would certainly jive with some of the patterns I've seen built around EF.
My take on it is that a repository is not constrained to a single business object, especially in a relational setting where changes to object state can influence relations, so the DbContext is the repository. The functionally important part (to me) is that changes are tracked by the context, not the sets, and that multiple sets can have simultaneous changes tracked by the context.
This is entirely my personal opinion, I know, but from a design standpoint it makes a lot more sense to me.
Reading it again, I did make a mis-statement on the "implement UoW" part. "Supports UoW" would have been a much more appropriate statement, as it's entirely contingent on how you actually use the context.
|
|
|
|
|
Hi All.
I Create a website in this the login control connect to my SQL server (in the same machine) to verify user name and passs....
The connectionstring
<add name="RNOCDBConnecRNOCDB" connectionstring="Data Source=RNOCDB\;Initial Catalog=RnocDB;Integrated Security=True" providername="System.Data.SqlClient">
When I Run
http://localhost:4212/Default.aspx
It OK
But if I change the localhost by the server IP it has error:
Server Error in '/RNOC' Application
Login failed for user 'RNOC\RNOCDB$'.
|
|
|
|
|
Looks like you are not able to connect to your SQL server in hosting. Is the server from hosting company? Are you able to connect to it using SSMS?
|
|
|
|
|
Does your db sever require credentials?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am not sure but I Config IIS Identity to Local System and it work.Thanks all.
|
|
|
|
|
I have a ASP.net page with an update panel triggered by a few imagebuttons (outside the updatepanel). All works OK.
Starting the page through a rewritemap.config however the imagebutton click is not executed, it becomes active but the click is not fired anymore.
I now do not know how to continu troubleshooting this problem, can anyone help me? Or provide a link where to look for more information?
Thanks in advance
Erik
|
|
|
|