2

I have a form in mvc where different fields settings are controlled from database.

I have written a razor function which checks if certain fields are mandatory.

In case of true I need to set the required attribute. Currently I can't call razor function in input tag.

What are my options?

<input type="text" class="form-control" asp-for="@Model.Title" required="@myfunction()" maxlength="200" />

2 Answers 2

2

You can create a tag helper for this type of situation like below

    private const string ForAttributeName = "asp-for";

    [HtmlAttributeName("asp-is-required")]
    public bool IsMandatory { set; get; }

    public InputTextRequired(IHtmlGenerator generator) : base(generator)
    {
    }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        if (IsMandatory)
        {
            var attribute = new TagHelperAttribute("required");
            output.Attributes.Add(attribute);
        }
        base.Process(context, output);
    }
}

You can use that tag helper in your input tag like below

<input type="text" class="form-control" asp-for="@Model.Title" asp-is-required="true/false from razor" maxlength="200" />

More info on Tag Helper use this link

Sign up to request clarification or add additional context in comments.

Comments

-2

If I understand correctly, you have your required parameter saved in database, so you can pass it from back end. I would recommend use Viewbag and send it to view from you controller. enter image description here

Then you can set required tag. enter image description here

6 Comments

"I would recommend use Viewbag" why? That doesn't sound like a scalable, maintainable solution.
@CodeCaste why it is not scalable, maintainble solution. Viewbag is used to send data from controller to view, that you dont need to post back after you fill your data.
Yes, and now you have ten properties on twenty models shown on thirty views, good luck maintaining that. You don't have static typing using the ViewBag. You'll have to be copy-pasting a lot of identifiers, which is error-prone. A custom validation attribute would seem more useful, where you write the logic once and let MVC do the rest. ViewBag is nice for one-off things, like a page title or a single-use dropdown for which you don't want to create a model, but apart from that, it's useless.
Yes, you can create custom annotation for your model, but then you have to add parameter to your model, that isn't data and anyway you have to assign that parameter everytime you want to use it, because you won't be able to select it from DB. I see one plus of your solution, that you can manage validation message easier.
@TomasKuzminskas you cannot pass any other value to required other than required. required ="" is equivalent to required is equivalent to requied = "required"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.