change the state of a textbox depending on which view is called - textbox

I'm new to MVC, and would like to know in C# mvc5, what kind of code I would need to use, if I wanted to change the state of a textbox from enabled to disabled and vice versa based on which view is calling the partial view.

Try to add something like this in your tag: #('Condition Here') ? disabled:"disabled" : ""
Or:
object mode = ('Condition Here') ? null : new {disabled = "disabled" };
#Html.TextBoxFor(x => x.YourPropertyHere, mode)

Related

WebGrid is returing an error "Column does not exist"

I have the following web grid inside my Razor view on asp.net MVC 5 and i am using Entity framework 6.0:-
Now the web grid is working well on all the paging except for one paging, and when i checked it i found that the WebGrid will return this error:-
Column "SDUser.Department.Definition.DEPTNAME" does not exist.
So seems some items inside this page does not have those navigation properties SDUser.Department.Definition.DEPTNAME,, so how i can overcome this issue?
EDIT:- Here is my updated code, where i added If/Else but still i am getting the same error:-
Before adding new WebGridColumn in gridcolumns, you check whether DEPTNAME property is there or not.
#if(#Model.Content.FirstOrDefault().SDUser.Department.Definition.HasProperty("DEPTNAME"))
{
gridcolumns.Add(new WebGridColumn()
{
ColumnName = "SDUser.Department.Definition.DEPTNAME",
Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().SDUser.Department.Definition.DEPTNAME).ToString(),
CanSort = true
});
}
Additionally, if you want to check its value is there or empty then use this
#if(#Model.Content.FirstOrDefault().SDUser.Department.Definition.HasProperty("DEPTNAME") && #Model.Content.FirstOrDefault().SDUser.Department.Definition.GetProperty("productSalePrice").Value != String.Empty)
{
//your code
}
Note: I don't know actually your hierarchy of Model, I consider that specific page has not DEPTNAME property. (you are free to modify according to your requirement)

asp.net selectbox gives null on postback

I am trying to create a user identifier and used a selectbox so they can choose between identifier methods. I did fine one other question here that addressed the situation, but after following that example it still posted back null.
It does show the selectbox correctly aswell as the containing data, however when i push save (post) the selected attr is not added and remains null.
code:
#Html.DropDownList("Authtype", (IEnumerable<SelectListItem>)ViewBag.Authtype)
#Html.DropDownListFor(model => model.Authtype, ViewBag.AvailableAuthorizationTypes as SelectList, "--- Select Category ---", new { #class = "some_class" })
both work (and yes show me the same result, I posted both in the hope that one might bring me further on the right track).
CreateIdentifierModel cim = new CreateIdentifierModel();
IEnumerable<SelectListItem> items = cim.AvailableAuthorizationTypes = Context.AuthorizationTypes.OrderBy(x => x.Name)
.Select(x => new SelectListItem() { Text = x.Name, Value = x.Identifier.ToString() });
ViewBag.Authtype = items;
return View();
When i click save it will go to the post method, but keeps the attr null, eventhough one of the options is selected.
It is probably a brain fart but I can't seem to work out where exactly the problem is and how to fix it. CAn someone help me out?
Kind regards,

Hide value in Html.textbox

A little question but I didn't find an answer, is it possible to pass and default hidden value in textbox ?
Explanation : So I have this piece of code :
#Html.TextBox("date", null, new { #class = "date" ,#Value =DateTime.Today.Date.Month})
In #Value, I'm putting a default value ( the month of the current date ) but I'd like this value not to be seen by the user ! I have tried to add a #Hidden = true but, as I expected, it's hidding the text box not the value.
So is there a way to only hide the value ?
Thanks in advance !
You could try to populate textbox with today's date by writing.
$(.date).daterangepicker({ minDate: new Date() });
It wont require any changes in textbox helper.
so it will not show you date like '01-01-0001' any more.
Hope this works.
I think you can set some attribute like defaultValue in input tag and than when you submit your form check if any value in that input than pass that value otherwise you can pass default value.
#Html.TextBox("date", null, new { #class = "date" ,DefaultValue =DateTime.Today.Date.Month})
javascript:when you submit the value first check this and set your value
if($('#date').val()=="")
{
$('#date').val($('#date').attr('DefaultValue'));
}

Using Html.DropDownListFor and selecting a value inside the SelectList will not work together

I am working on an asp.net mvc-5 web application and i wrote the following inside my Get Edit action method to buld a dropdownlist:-
var t=await unitofwork.SkillRepository.getallSkillCategories().ToListAsync();
ViewBag.SkillCategoryID = new SelectList(t, "SkillCategoryID", "Name",skill.SkillCategoryID);
and inside the view i wrote the following:-
#Html.DropDownListFor(model => model.SkillCategoryID,(SelectList) ViewBag.SkillCategoryID,"--please select--", null)
but the above did not work together , as the current SkillCategoryID will not be selected , and i will always get "--please select--". to get this fixed i have to do one of the following:-
1)to modify my action method as follow (remove the skill.SkillCategoryID):-
ViewBag.SkillCategoryID = new SelectList(t, "SkillCategoryID", "Name");
2)or to modify my view to pass null instead of mentioning the ViewBag:-
#Html.DropDownListFor(model => model.SkillCategoryID,null,"--please select--", null)
so why my first approach did not work ??

Umbraco 6.2.0 Render maco inside a razor macroscript

I have the below code which works but is there a better way using the RenderMacroContent method. Not sure how to add parameters to that.
<umbraco:Macro runat="server" language="cshtml">#{
HtmlTextWriter writer = new HtmlTextWriter(this.Output);
var macroPressLogin = new umbraco.presentation.templateControls.Macro();
macroPressLogin.Alias = "Security-PressLogin";
macroPressLogin.Attributes.Add("TargetNode", Parameter.TargetNode);
macroPressLogin.RenderControl(writer); }</umbraco:Macro>
As long as your view inherits from Umbraco.Web.Mvc.UmbracoTemplatePage or Umbraco.Web.Mvc.UmbracoViewPage<YourModel> you should just be able to write something like:
#Umbraco.RenderMacro("MacroAlias", new { ParameterAlias1 = "value1", ParameterAlias2 = "value2" })
So in your case it would be:
#Umbraco.RenderMacro("Security-PressLogin", new { TargetNode = "targetNodeValue" })
If you're talking about calling one macro from within another then this should also work as long as your macro partial view inherits from Umbraco.Web.Macros.PartialViewMacroPage
From your example it looks like you're working with a legacy razor macro using umbraco.MacroEngines. If possible I would recommend upgrading to a partial view macro. Click here for some further info.
I know this question is a year old, but I hope someone finds a use for this information (using Umbraco version 6.x):
#(Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"MacroAliasHere\" dynamicParameter=\""+ #dynamicValue + "\" staticParameter=\"staticValue\" ></?UMBRACO_MACRO>", Model.Id)))
Option number 2, which I prefer, is actually code to output a contour form using a value from the form picker datatype:
var helper = new UmbracoHelper(UmbracoContext.Current);
#Html.Raw(helper.RenderMacro("umbracoContour.RazorRenderForm", new { formGuid = #Model.formPickerAlias }))

Resources